結果
問題 | No.665 Bernoulli Bernoulli |
ユーザー | FF256grhy |
提出日時 | 2018-04-11 23:56:51 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,434 ms / 2,000 ms |
コード長 | 3,150 bytes |
コンパイル時間 | 1,925 ms |
コンパイル使用メモリ | 169,348 KB |
実行使用メモリ | 391,204 KB |
最終ジャッジ日時 | 2024-06-26 21:18:00 |
合計ジャッジ時間 | 24,690 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1,434 ms
391,048 KB |
testcase_03 | AC | 1,430 ms
391,204 KB |
testcase_04 | AC | 1,351 ms
379,980 KB |
testcase_05 | AC | 1,266 ms
365,668 KB |
testcase_06 | AC | 1,256 ms
362,760 KB |
testcase_07 | AC | 1,207 ms
358,124 KB |
testcase_08 | AC | 1,211 ms
357,348 KB |
testcase_09 | AC | 1,374 ms
382,124 KB |
testcase_10 | AC | 1,202 ms
358,032 KB |
testcase_11 | AC | 1,418 ms
386,748 KB |
testcase_12 | AC | 1,363 ms
381,404 KB |
testcase_13 | AC | 1,409 ms
389,380 KB |
testcase_14 | AC | 1,427 ms
390,856 KB |
testcase_15 | AC | 1,237 ms
363,488 KB |
testcase_16 | AC | 1,297 ms
371,284 KB |
testcase_17 | AC | 1,261 ms
365,816 KB |
testcase_18 | AC | 1,213 ms
359,156 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long signed int LL; typedef long long unsigned int LU; #define incID(i, l, r) for(int i = (l) ; i < (r); i++) #define incII(i, l, r) for(int i = (l) ; i <= (r); i++) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--) #define decII(i, l, r) for(int i = (r) ; i >= (l); i--) #define inc(i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec(i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define PQ priority_queue #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define FOR(it, v) for(auto it = v.begin(); it != v.end(); ++it) #define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it) template<typename T> bool setmin(T & a, T b) { if(b < a) { a = b; return true; } else { return false; } } template<typename T> bool setmax(T & a, T b) { if(b > a) { a = b; return true; } else { return false; } } template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } } template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } } template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } // ---- ---- LL MOD; LL mod(LL x, LL m = MOD) { return (x % m + m) % m; } pair<LL, LL> ex_gcd(LL a, LL b) { if(b == 0) { return MP(1, 0); } auto p = ex_gcd(b, a % b); return MP(p.SE, p.FI - (a / b) * p.SE); } LL inv(LL x, LL m = MOD) { assert(gcd(x, m) == 1); auto p = ex_gcd(x, m); return mod(p.FI, m); } LL promod(LL x, LL y, LL m = MOD) { return mod((x % m) * (y % m), m); } LL divmod(LL x, LL y, LL m = MOD) { return promod(x, inv(y, m), m); } LL ex(LL x, LL y, LL mod = MOD) { LL z[64], v = 1; inc(i, 64) { z[i] = (i == 0 ? x : z[i - 1] * z[i - 1]) % mod; } inc(i, 64) { if((y >> i) & 1) { (v *= z[i]) %= mod; } } return v; } // ---- ---- const int LIM = 10000; int C[LIM + 2][LIM + 2]; LL B[LIM + 2]; void calc_comb(LL n, LL m) { incII(i, 0, n) { incII(j, 0, i) { C[i][j] = (i == 0 || j == 0 ? 1 : (C[i - 1][j] + C[i - 1][j - 1])) % m; } } } void calc_bernoulli_number_mod(LL n, LL m, int sgn = -1) { // calc: [0, n], m: prime number // sgn == -1: B- (B(1) == -1/2) // sgn == +1: B+ (B(1) == +1/2) assert(abs(sgn) == 1); incII(i, 0, n) { if(i == 0) { B[0] = 1; } else { B[i] = 0; inc(k, i) { (B[i] += C[i + 1][k] * B[k]) %= m; } B[i] = divmod(-B[i], i + 1, m); } } if(sgn == +1) { B[1] = divmod(+1, 2, m); } } LL sum_power(LL n, LL k, LL m) { // (1^k + 2^k + ... + n^k) % m assert(k <= LIM); calc_comb(k + 1, m); calc_bernoulli_number_mod(k, m, +1); LL ans = 0; incII(i, 0, k) { (ans += C[k + 1][i] * B[i] % m * ex(n, k + 1 - i, m)) %= m; } return divmod(ans, k + 1, m); } int main() { LL n, k; cin >> n >> k; cout << sum_power(n, k, 1e9 + 7) << endl; return 0; }