結果

問題 No.391 CODING WAR
ユーザー ayanamizutaayanamizuta
提出日時 2017-09-25 08:15:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 808 bytes
コンパイル時間 572 ms
コンパイル使用メモリ 56,984 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-26 21:32:00
合計ジャッジ時間 1,393 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

#define LL long long
#define MOD 1000000007
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)

LL pow(LL n, LL m){
  LL a = 1;
  LL i,prod;
  prod=n;
  while(m > 0){
    if(m % 2 == 1)
      a = a*prod % MOD;
    prod = prod*prod %MOD;
    m /= 2;
  }
  return a;
}

LL modinv(LL a, LL p){
  return pow(a,p-2) % p;
}

LL ans(LL n, LL m){
  LL val = pow(m,n);
  LL i;
  LL com = 1;
  FOR(i,1,m){
    //cout << i << " "<< val << " " << com <<endl;
    com = ((com*(m-i+1)) % MOD) * modinv(i,MOD) % MOD;
    val = (m-i)%2==0 ? (val + com*pow(i,n)) % MOD : (MOD + (val - com*pow(i,n)) % MOD) % MOD;
  }
  return val;
}

int main(){
  LL n,m;
  cin >> n >> m;
  if(n >= m)
    cout << ans(n,m) << endl;
  else
    cout << 0 << endl;
  return 0;
}
0