結果

問題 No.665 Bernoulli Bernoulli
ユーザー FF256grhyFF256grhy
提出日時 2018-04-11 23:56:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,435 ms / 2,000 ms
コード長 3,150 bytes
コンパイル時間 1,459 ms
コンパイル使用メモリ 168,524 KB
実行使用メモリ 392,428 KB
最終ジャッジ日時 2023-09-09 04:11:23
合計ジャッジ時間 24,677 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1,435 ms
392,428 KB
testcase_03 AC 1,430 ms
392,416 KB
testcase_04 AC 1,355 ms
382,488 KB
testcase_05 AC 1,264 ms
368,164 KB
testcase_06 AC 1,246 ms
366,168 KB
testcase_07 AC 1,206 ms
359,972 KB
testcase_08 AC 1,207 ms
359,968 KB
testcase_09 AC 1,369 ms
384,596 KB
testcase_10 AC 1,207 ms
360,032 KB
testcase_11 AC 1,400 ms
388,844 KB
testcase_12 AC 1,362 ms
382,488 KB
testcase_13 AC 1,417 ms
390,688 KB
testcase_14 AC 1,427 ms
391,556 KB
testcase_15 AC 1,240 ms
366,376 KB
testcase_16 AC 1,297 ms
374,424 KB
testcase_17 AC 1,258 ms
368,444 KB
testcase_18 AC 1,213 ms
361,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0