結果

問題 No.391 CODING WAR
ユーザー face4face4
提出日時 2019-03-02 16:36:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,332 bytes
コンパイル時間 524 ms
コンパイル使用メモリ 71,432 KB
実行使用メモリ 4,908 KB
最終ジャッジ日時 2023-09-05 16:55:48
合計ジャッジ時間 2,530 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,620 KB
testcase_01 AC 4 ms
4,704 KB
testcase_02 AC 4 ms
4,716 KB
testcase_03 AC 4 ms
4,652 KB
testcase_04 AC 4 ms
4,664 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 4 ms
4,556 KB
testcase_07 AC 4 ms
4,652 KB
testcase_08 AC 4 ms
4,908 KB
testcase_09 AC 125 ms
4,596 KB
testcase_10 AC 88 ms
4,860 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 4 ms
4,628 KB
testcase_13 AC 113 ms
4,640 KB
testcase_14 AC 106 ms
4,600 KB
testcase_15 AC 122 ms
4,736 KB
testcase_16 AC 75 ms
4,636 KB
testcase_17 AC 79 ms
4,648 KB
testcase_18 AC 60 ms
4,688 KB
testcase_19 AC 59 ms
4,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;

ll modpow(ll a, ll b, ll p = 1e9+7){
    if(b == 0)  return 1;

    if(b % 2 == 0){
        ll d = modpow(a, b/2, p);
        return (d*d) % p;
    }else{
        return (a%p * modpow(a, b-1, p)) % p;
    }
}

struct ModComb{
    vector<ll> po, inv;
    ll N;

    ModComb(ll n) : N(n), po(n), inv(n) {
        po[0] = 1;
        for(int i = 1; i < N; i++)  po[i] = (po[i-1] * i) % mod;
        inv[N-1] = modpow(po[N-1], mod-2, mod);
        for(int i = N-2; i >= 0; i--)   inv[i] = (inv[i+1] * (i+1)) % mod;
    }
    
    ll nCk(ll n, ll k){
        if(n < k)   return 0;
        return (((po[n]*inv[n-k])%mod)*inv[k])%mod;
    }

    ll nPk(ll n, ll k){
        if(n < k)   return 0;
        return (po[n]*inv[n-k])%mod;
    }

    ll nHk(ll n, ll k){
        if(n == 0 && k == 0)    return 1;
        return nCk(n+k-1, k);
    }
};

int main(){
    ll n, m;
    cin >> n >> m;
    
    if(n < m){
        cout << 0 << endl;
        return 0;
    }

    ModComb mc(100001);
    ll ans = modpow(m, n, mod);

    for(ll i = 1; i < m; i++){
        ans += (-2*(i%2)+1) * ((mc.nCk(m, i)*modpow(m-i, n, mod))%mod);
        if(ans < 0) ans += (-ans/mod+1)*mod;
        ans %= mod;
    }

    cout << ans << endl;

    return 0;
}
0