結果

問題 No.391 CODING WAR
ユーザー btkbtk
提出日時 2016-07-08 23:18:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,278 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 165,248 KB
実行使用メモリ 15,828 KB
最終ジャッジ日時 2023-08-03 09:25:34
合計ジャッジ時間 3,108 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
15,728 KB
testcase_01 AC 13 ms
15,596 KB
testcase_02 AC 13 ms
15,656 KB
testcase_03 AC 13 ms
15,644 KB
testcase_04 AC 13 ms
15,756 KB
testcase_05 AC 13 ms
15,612 KB
testcase_06 AC 13 ms
15,644 KB
testcase_07 AC 13 ms
15,712 KB
testcase_08 AC 13 ms
15,660 KB
testcase_09 AC 64 ms
15,592 KB
testcase_10 AC 63 ms
15,660 KB
testcase_11 AC 13 ms
15,696 KB
testcase_12 AC 13 ms
15,652 KB
testcase_13 AC 60 ms
15,776 KB
testcase_14 AC 50 ms
15,740 KB
testcase_15 AC 55 ms
15,704 KB
testcase_16 AC 38 ms
15,796 KB
testcase_17 AC 44 ms
15,604 KB
testcase_18 AC 33 ms
15,676 KB
testcase_19 AC 34 ms
15,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

struct INIT{INIT(){ios::sync_with_stdio(false);cin.tie(0);cout<<fixed;cout<<setprecision(10);}}init;
#define rep(i,n) for(auto i=(n)*0;i<n;i++)
#define range(it,v) for(auto &it:v)


typedef long long LL;
const int MOD=(int)(1e9+7);

LL fact[2000000];
struct init2{
    init2(){
	fact[0]=1;
	rep(i,1500000)fact[i+1]=fact[i]*(i+1)%MOD;
    }
}i2;
// a x + b y = gcd(a, b)
// O(log (a+b) )
LL extgcd(LL a, LL b, LL &x, LL &y) {
  LL g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

// mを法とするaの逆元
// O(log a)
LL invMod(LL a) {
	LL x, y;
	if (extgcd(a, MOD, x, y) == 1)return (x + MOD) % MOD;
	else	 return 0; // unsolvable
}


LL Comb(LL n,LL k){
	LL u=fact[n];
	LL d=(fact[k]*fact[n-k])%MOD;
	return (u*invMod(d))%MOD;
}


//a^n (mod MOD)
//O(log a)
LL pow_mod(LL a,LL n){
    a%=MOD;
    LL res=1;
    LL b=1;
    while(n>=b){
	if(n&b)
	    res=(res*a)%MOD;
	a=(a*a)%MOD;
	b<<=1;
	}
    return res;
}


int main(){
    LL N,M;
    cin>>N>>M;
    if(N<M)cout<<0<<endl;
    else {
	LL res=0;
	for(int i=0;i<M;i++)
	    if(i&1)res=(res+MOD-Comb(M,M-i)*pow_mod(M-i,N)%MOD)%MOD;
	    else res=(res+Comb(M,M-i)*pow_mod(M-i,N)%MOD)%MOD;
	cout<<res<<endl;
    }
    return 0;
}
0