結果

問題 No.665 Bernoulli Bernoulli
ユーザー 0w10w1
提出日時 2018-03-15 14:19:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,172 bytes
コンパイル時間 2,550 ms
コンパイル使用メモリ 201,180 KB
実行使用メモリ 5,708 KB
最終ジャッジ日時 2023-08-22 13:10:03
合計ジャッジ時間 3,592 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAXN = int( 1e9 );
const int MAXK = int( 1e6 );
const int MOD = int( 1e9 ) + 7;

int int_pow( int v, int p ) {
  int res = not ( v == 0 and p );
  while( p ) {
    if( p & 1 ) {
      res = 1LL * res * v % MOD;
    }
    p >>= 1;
    v = 1LL * v * v % MOD;
  }
  return res;
}

int inv( int v ) {
  return int_pow( v, MOD - 2 );
}

int fact[ MAXK + 1 + 1 ];

int N, K;

int f[ MAXK + 2 + 1 ];

signed main() {
  ios::sync_with_stdio( 0 );
  cin >> N >> K;
  for( int i = fact[ 0 ] = 1; i <= K + 1; ++i ) {
    fact[ i ] = 1LL * fact[ i - 1 ] * i % MOD;
  }
  for( int i = 1; i <= K + 2; ++i ) {
    f[ i ] = ( f[ i - 1 ] + int_pow( i, K ) ) % MOD;
  }
  if( N <= K + 2 ) {
    cout << f[ N ] << endl;
    exit( 0 );
  }
  int xmj = 1;
  for( int i = 1; i <= K + 2; ++i ) {
    xmj = 1LL * xmj * ( N - i ) % MOD;
  }
  int ans = 0;
  for( int i = 1; i <= K + 2; ++i ) {
    int p = 1LL * f[ i ] * xmj % MOD * inv( N - i ) % MOD * inv( 1LL * fact[ i - 1 ] * fact[ K + 2 - i ] % MOD ) % MOD;
    if( K + 2 - i & 1 ) p *= -1;
    ( ans += p ) %= MOD;
  }
  if( ans < 0 ) ans += MOD;
  cout << ans << endl;
  return 0;
}
0