結果
| 問題 |
No.1035 Color Box
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-15 19:38:24 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 2,000 ms |
| コード長 | 1,124 bytes |
| コンパイル時間 | 2,468 ms |
| コンパイル使用メモリ | 211,516 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-22 17:31:11 |
| 合計ジャッジ時間 | 3,921 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 |
ソースコード
import std;
int readInt() {
int x;
readf!" %d"(x);
return x;
}
int[] readArray(int n) {
auto a = new int[](n);
return a.map!(i => readInt).array;
}
immutable long P = (1e9 + 7).to!long;
struct Combinatorics(long P) {
long[] inv, fac, ifac;
this(int n) {
inv = new long[](n + 1);
fac = new long[](n + 1);
ifac = new long[](n + 1);
inv[1] = fac[0] = ifac[0] = 1;
for(int i = 2; i <= n; i ++)
inv[i] = (P - P / i) * inv[P % i] % P;
for(int i = 1; i <= n; i ++) {
fac[i] = i * fac[i - 1] % P;
ifac[i] = inv[i] * ifac[i - 1] % P;
}
}
long binom(int n, int m) {
if(n < m || m < 0) return 0; // n >= 0
return fac[n] * ifac[m] % P * ifac[n - m] % P;
}
long qpow(long a, long b) {
long r = 1, t = a % P;
for(; b; b /= 2) {
if(b & 1)
r = r * t % P;
t = t * t % P;
}
return r;
}
};
void main() {
int n = readInt, m = readInt;
auto comb = new Combinatorics!P(n);
long ans = comb.qpow(m, n);
foreach(i; 0 .. m) {
int sgn = ((m - i) & 1) ? -1 : 1;
ans += sgn * comb.qpow(i, n) * comb.binom(m, i);
ans %= P;
}
ans %= P;
if(ans < 0) ans += P;
writeln(ans);
}