結果
| 問題 | No.1035 Color Box |
| コンテスト | |
| ユーザー |
ks2m
|
| 提出日時 | 2020-04-24 22:11:52 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 203 ms / 2,000 ms |
| コード長 | 1,296 bytes |
| コンパイル時間 | 2,280 ms |
| コンパイル使用メモリ | 77,760 KB |
| 実行使用メモリ | 56,328 KB |
| 最終ジャッジ日時 | 2024-10-15 02:58:56 |
| 合計ジャッジ時間 | 9,206 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 |
ソースコード
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
sc.close();
int mod = 1000000007;
NCR ncr = new NCR(n, mod);
long ans = power(m, n, mod);
boolean odd = true;
for (int i = m - 1; i >- 1; i--) {
long rem = power(i, n, mod) * ncr.calc(m, i) % mod;
if (odd) {
rem = -rem;
}
ans += rem;
odd = !odd;
}
while (ans < 0) {
ans += mod;
}
System.out.println(ans % mod);
}
static class NCR {
long[] p, pi;
int m;
public NCR(int n, int mod) {
n++;
m = mod;
p = new long[n];
pi = new long[n];
p[0] = 1;
pi[0] = 1;
for (int i = 1; i < n; i++) {
p[i] = p[i - 1] * i % m;
}
pi[n - 1] = BigInteger.valueOf(p[n - 1])
.modInverse(BigInteger.valueOf(m)).longValue();
for (int i = n - 1; i > 1; i--) {
pi[i - 1] = pi[i] * i % m;
}
}
public long calc(int n, int r) {
if (n < r) return 0;
return p[n] * pi[r] % m * pi[n - r] % m;
}
}
static long power(long x, long n, int m) {
if (n == 0) {
return 1;
}
long val = power(x, n / 2, m);
val = val * val % m;
if (n % 2 == 1) {
val = val * x % m;
}
return val;
}
}
ks2m