結果

問題 No.391 CODING WAR
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-06-12 15:23:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,822 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 95,708 KB
実行使用メモリ 6,784 KB
最終ジャッジ日時 2024-04-19 03:53:21
合計ジャッジ時間 2,706 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
6,656 KB
testcase_01 AC 31 ms
6,784 KB
testcase_02 AC 32 ms
6,656 KB
testcase_03 AC 32 ms
6,528 KB
testcase_04 AC 32 ms
6,656 KB
testcase_05 AC 32 ms
6,656 KB
testcase_06 AC 32 ms
6,528 KB
testcase_07 AC 32 ms
6,784 KB
testcase_08 AC 32 ms
6,528 KB
testcase_09 AC 50 ms
6,656 KB
testcase_10 AC 49 ms
6,528 KB
testcase_11 AC 39 ms
6,784 KB
testcase_12 AC 32 ms
6,656 KB
testcase_13 AC 47 ms
6,656 KB
testcase_14 AC 44 ms
6,784 KB
testcase_15 AC 47 ms
6,656 KB
testcase_16 AC 41 ms
6,656 KB
testcase_17 AC 42 ms
6,784 KB
testcase_18 AC 39 ms
6,656 KB
testcase_19 AC 39 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;

const ll MOD = 1000000007;
const int N_MAX = 200000;
const long double EPS = 1e-10;
const int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

ll modpow(ll a, ll t) {
    ll ret = 1LL;
    while(t){
        if(t & 1LL){
            ret *= a;
            ret %= MOD;
        }
        a *= a;
        a %= MOD;
        t >>= 1;
    }
    return ret;
}

ll fact[N_MAX], rfact[N_MAX];

ll perm(ll n, ll r){
    return (fact[n] * rfact[r]) % MOD;
}

ll comb(ll n, ll r){
    return (perm(n, r) * rfact[n-r]) % MOD;
}

void init(ll n){
    fact[0] = fact[1] = 1;
    rfact[0] = rfact[1] = 1;
    for(int i=2;i<=n;++i) {
        fact[i] = (fact[i-1] * (ll)i) % MOD;
        rfact[i] = 1;
        ll k = MOD-2;
        ll a = fact[i];
        while(k > 0){
            if(k & 1){
                rfact[i] *= a;
                rfact[i] %= MOD;
            }
            a *= a;
            a %= MOD;
            k  >>= 1;
        }
    }
}

int main() {
    std::ios::sync_with_stdio(0); cin.tie(0);
    ll n, m;
    cin >> n >> m;

    init(N_MAX);

    ll ans = modpow(m, n);
    for(int i=1;i<m;++i) {
        ll tmp = (comb(m, i) * modpow(m-i, n)) % MOD;
        if(i%2) {
            ans += MOD - tmp;
        } else {
            ans += tmp;
        }
        ans %= MOD;
    }

    cout << ans << endl;

}
0