結果

問題 No.665 Bernoulli Bernoulli
コンテスト
ユーザー bal4u
提出日時 2019-07-20 11:18:22
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,087 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 474 ms
コンパイル使用メモリ 41,388 KB
最終ジャッジ日時 2026-02-22 03:58:00
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: No.665 Bernoulli Bernoulli
// 2019.7.20 bal4u

#include <stdio.h>

typedef long long ll;
const int M = 1000000007;

int powmod(int b, int p) {
   int r = 1;
   while (p) {
      if (p & 1) r = (ll)r * b % M;
      b = (ll)b * b % M;
	  p >>= 1;
   }
   return r;
}

int egcd(int a, int b, int *x, int *y) {
	int d;
	if (b == 0) { *x = 1; *y = 0; return a; }
	d = egcd(b, a % b, y, x);
	*y -= a / b * (*x);
	return d;
}

int inv(int a) {
    int x, y;
    egcd(a, M, &x, &y);
	return x % M;
}

int fact[10005] = {1};
int xpow[10005];

int main()
{
	int i, N, K, K1, ans;
	
	{ ll _; scanf("%lld%d", &_, &K), N = (int)(_ % M), K1 = K+1; }
	
	for (i = 1; i <= K1; i++) {
		fact[i] = (ll)fact[i-1]*i % M;
		xpow[i] = (xpow[i-1] + powmod(i, K)) % M;
	}
	if (N <= K1) { printf("%d\n", xpow[N]); return 0; }

	ans = 0; for (i = 1; i <= K1; i++) {
		ll t = (ll)xpow[i] * inv((ll)fact[i]*fact[K1-i] % M * (N-i) % M);
		if (!((i&1) ^ (K&1))) t = -t;
		ans = (t + ans) % M;
	}
	for (i = N-K1; i <= N; i++) ans = (ll)ans*i % M;
	if (ans < 0) ans += M;
	printf("%d\n", ans);
	return 0;
}
0