結果

問題 No.665 Bernoulli Bernoulli
ユーザー 👑 kmjpkmjp
提出日時 2018-03-09 23:05:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,369 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 160,760 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 18:02:15
合計ジャッジ時間 2,272 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 18 ms
5,376 KB
testcase_04 AC 18 ms
5,376 KB
testcase_05 AC 18 ms
5,376 KB
testcase_06 AC 18 ms
5,376 KB
testcase_07 AC 17 ms
5,376 KB
testcase_08 AC 17 ms
5,376 KB
testcase_09 AC 18 ms
5,376 KB
testcase_10 AC 17 ms
5,376 KB
testcase_11 AC 18 ms
5,376 KB
testcase_12 AC 18 ms
5,376 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 17 ms
5,376 KB
testcase_15 AC 16 ms
5,376 KB
testcase_16 AC 18 ms
5,376 KB
testcase_17 AC 17 ms
5,376 KB
testcase_18 AC 17 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll N,K;
ll mo=1000000007;
ll P[101010];
ll fact[101010];

ll modpow(ll a, ll n = mo-2) {
	ll r=1;a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	
	ll ret=0;
	if(N<=K+2) {
		for(i=1;i<=N;i++) ret+=modpow(i,K);
		cout<<ret%mo<<endl;
		return;
	}
	
	for(i=1;i<=K+1;i++) (P[i]=modpow(i,K)+P[i-1])%=mo;
	fact[0]=1;
	for(i=1;i<=K+1;i++) fact[i]=fact[i-1]*i%mo;
	
	ll A=1,B=1;
	for(i=0;i<=K+1;i++) A=A*(N%mo-i+mo)%mo;
	
	for(i=0;i<=K+1;i++) {
		ll v=P[i]*A%mo*modpow(N%mo-i+mo)%mo;
		ll w=fact[K+1-i]*fact[i]%mo;
		v=v*modpow(w)%mo;
		
		if(i%2 != (K+1)%2) ret=(ret+mo-v)%mo;
		else ret=(ret+v)%mo;
	}
	
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0