結果

問題 No.665 Bernoulli Bernoulli
ユーザー どららどらら
提出日時 2018-03-11 01:39:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 1,689 bytes
コンパイル時間 1,798 ms
コンパイル使用メモリ 168,576 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 00:50:32
合計ジャッジ時間 7,821 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
5,248 KB
testcase_01 AC 8 ms
5,376 KB
testcase_02 AC 349 ms
5,376 KB
testcase_03 AC 350 ms
5,376 KB
testcase_04 AC 334 ms
5,376 KB
testcase_05 AC 316 ms
5,376 KB
testcase_06 AC 312 ms
5,376 KB
testcase_07 AC 295 ms
5,376 KB
testcase_08 AC 305 ms
6,944 KB
testcase_09 AC 342 ms
6,944 KB
testcase_10 AC 297 ms
6,940 KB
testcase_11 AC 354 ms
6,940 KB
testcase_12 AC 335 ms
6,940 KB
testcase_13 AC 353 ms
6,940 KB
testcase_14 AC 354 ms
6,944 KB
testcase_15 AC 301 ms
6,944 KB
testcase_16 AC 312 ms
6,940 KB
testcase_17 AC 310 ms
6,944 KB
testcase_18 AC 297 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1000000007LL;
const int MX = 10005;

ll fact[MX];
ll invfact[MX];

ll extgcd(ll a, ll b, ll &x, ll &y){
  ll d = a;
  if(b!=0){
    d = extgcd(b,a%b,y,x);
    y -= (a/b)*x;
  }else{
    x = 1; y = 0;
  }
  return d;
}

ll mod_inverse(ll a, ll m){
  ll x, y;
  extgcd(a, m, x, y);
  return (m+x%m)%m;
}

ll mod_pow(ll x, ll n, ll mod){
  if(n==0) return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if(n & 1) res = res * x % mod;
  return res;
}

ll mod_comb(ll n, ll k, ll p){
  if(n<0 || k<0 || n<k) return 0;
  return (((fact[n] * invfact[k]) % MOD) * invfact[n-k]) % MOD;
}

void comb_init(){
  fact[0] = fact[1] = 1;
  for(ll i=2; i<MX; i++) fact[i] = (fact[i-1] * i) % MOD;
  invfact[0] = invfact[1] = 1;
  for(ll i=2; i<MX; i++) invfact[i] = mod_pow(fact[i], MOD-2, MOD);
}


int main(){
  ll n, k;
  cin >> n >> k;

  comb_init();

  vector<ll> B;
  B.push_back(1);
  REP(i,1,k+1){
    ll sum = 0;
    rep(j,i){
      sum += (mod_comb(i+1,j,MOD) * B[j]) % MOD;
      sum %= MOD;
    }
    sum *= mod_inverse(i+1, MOD);
    sum %= MOD;
    sum *= -1;
    sum += MOD;
    B.push_back(sum);
  }

  n++;

  ll ret = 0;
  rep(j,k+1){
    ll x = mod_comb(k+1,j, MOD);
    x *= B[j];
    x %= MOD;
    x *= mod_pow(n%MOD, k-j+1, MOD);
    x %= MOD;
    ret += x;
    ret %= MOD;
  }

  ret *= mod_inverse(k+1, MOD);
  ret %= MOD;

  cout << ret << endl;

  return 0;
}
0