一、分割序列

import java.util.*;
public class Main {
static final long MOD = 1000000007L;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
List<Integer> pos = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '1') {
pos.add(i);
}
}
if (pos.isEmpty()) {
System.out.println(0);
return;
}
long ans = 1;
for (int i = 1; i < pos.size(); i++) {
long diff = pos.get(i) - pos.get(i - 1);
ans = ans * diff % MOD;
}
System.out.println(ans);
}
}
运行结果

二、礼物清单

import java.util.Scanner;
public class Main {
static final long MOD = 1000000007L;
static long qpow(long a, long b, long mod) {
long res = 1;
a %= mod;
while (b > 0) {
if (b % 2 == 1) {
res = res * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long m = sc.nextLong();
long base = qpow(2, m, MOD);
base = (base - 1 + MOD) % MOD;
long ans = qpow(base, n, MOD);
System.out.println(ans);
}
}
运行结果

三、矩形计数

import java.util.Scanner;
public class Main {
static final int MOD = 1000000007;
static final int MAX = 1000;
static long[] pow2 = new long[MAX + 1];
static void pre() {
pow2[0] = 1;
for (int i = 1; i <= MAX; i++) {
pow2[i] = pow2[i - 1] * 2 % MOD;
}
}
public static void main(String[] args) {
pre();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] a = new int[n][m];
int[] row0 = new int[n];
int[] row1 = new int[n];
int[] col0 = new int[m];
int[] col1 = new int[m];
long totalSingle = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = sc.nextInt();
if (a[i][j] == 0) {
row0[i]++;
col0[j]++;
} else {
row1[i]++;
col1[j]++;
}
totalSingle++;
}
}
long sumRow = 0;
for (int i = 0; i < n; i++) {
long s0 = (pow2[row0[i]] - 1 + MOD) % MOD;
long s1 = (pow2[row1[i]] - 1 + MOD) % MOD;
sumRow = (sumRow + s0 + s1) % MOD;
}
long sumCol = 0;
for (int j = 0; j < m; j++) {
long s0 = (pow2[col0[j]] - 1 + MOD) % MOD;
long s1 = (pow2[col1[j]] - 1 + MOD) % MOD;
sumCol = (sumCol + s0 + s1) % MOD;
}
long ans = (sumRow + sumCol - totalSingle % MOD + MOD) % MOD;
System.out.println(ans);
}
}
运行结果

四、【模板】组合数

import java.io.*;
public class Main {
static final int MOD = 1000000007;
static final int MAX = 500000;
static long[] fact = new long[MAX + 1];
static long[] invfact = new long[MAX + 1];
static long qpow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
static void pre() {
fact[0] = 1;
for (int i = 1; i <= MAX; i++) {
fact[i] = fact[i - 1] * i % MOD;
}
invfact[MAX] = qpow(fact[MAX], MOD - 2);
for (int i = MAX - 1; i >= 0; i--) {
invfact[i] = invfact[i + 1] * (i + 1) % MOD;
}
}
public static void main(String[] args) throws IOException {
pre();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
while (T-- > 0) {
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
long ans = fact[m] * invfact[n] % MOD;
ans = ans * invfact[m - n] % MOD;
bw.write(ans + "\n");
}
bw.flush();
}
}
运行结果

五、人员分组问题

import java.util.Scanner;
public class Main {
static final long MOD = 1000000007L;
static long inv120, inv720, inv5040;
static long qpow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
static void pre() {
inv120 = qpow(120, MOD - 2);
inv720 = qpow(720, MOD - 2);
inv5040 = qpow(5040, MOD - 2);
}
public static void main(String[] args) {
pre();
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long c5 = 0, c6 = 0, c7 = 0;
if (n >= 5) {
c5 = n * (n - 1) % MOD;
c5 = c5 * (n - 2) % MOD;
c5 = c5 * (n - 3) % MOD;
c5 = c5 * (n - 4) % MOD;
c5 = c5 * inv120 % MOD;
}
if (n >= 6) {
c6 = c5 * (n - 5) % MOD;
c6 = c6 * 6 % MOD;
c6 = c6 * inv720 % MOD;
}
if (n >= 7) {
c7 = c6 * (n - 6) % MOD;
c7 = c7 * 7 % MOD;
c7 = c7 * inv5040 % MOD;
}
long ans = (c5 + c6 + c7) % MOD;
System.out.println(ans);
}
}
运行结果

1635



被折叠的 条评论
为什么被折叠?



