結果

問題 No.665 Bernoulli Bernoulli
ユーザー どららどらら
提出日時 2018-03-12 22:08:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 2,037 bytes
コンパイル時間 1,709 ms
コンパイル使用メモリ 166,412 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-26 22:15:12
合計ジャッジ時間 10,283 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 382 ms
5,248 KB
testcase_01 AC 382 ms
5,376 KB
testcase_02 AC 386 ms
5,376 KB
testcase_03 AC 392 ms
5,376 KB
testcase_04 AC 394 ms
5,376 KB
testcase_05 AC 394 ms
5,376 KB
testcase_06 AC 380 ms
5,376 KB
testcase_07 AC 381 ms
5,376 KB
testcase_08 AC 391 ms
5,376 KB
testcase_09 AC 387 ms
5,376 KB
testcase_10 AC 384 ms
5,376 KB
testcase_11 AC 388 ms
5,376 KB
testcase_12 AC 389 ms
5,376 KB
testcase_13 AC 387 ms
5,376 KB
testcase_14 AC 385 ms
5,376 KB
testcase_15 AC 384 ms
5,376 KB
testcase_16 AC 389 ms
5,376 KB
testcase_17 AC 387 ms
5,376 KB
testcase_18 AC 388 ms
5,376 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;

template<ll MOD, ll MX>
class SumOfPower {
  ll fact[MX];
  ll invfact[MX];
  ll B[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);
  }

  void calc_bernoulli(){
    B[0] = 1;
    for(int i=1; i<MX; i++){
      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[i] = sum;
    }
  }
  
public:
  SumOfPower(){
    comb_init();
    calc_bernoulli();
  }

  // (1^k + ... + n^k) mod MOD, k<MX
  ll solve(ll n, ll k){
    n++;
    ll ret = 0;
    for(int j=0; j<=k; j++){
      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;
    return ret;
  }
};

int main(){
  ll n, k;
  cin >> n >> k;
  
  SumOfPower<1000000007LL,10005> sop;
  
  cout << sop.solve(n, k) << endl;

  return 0;
}

0