結果

問題 No.391 CODING WAR
ユーザー hiyokko2hiyokko2
提出日時 2017-07-30 16:36:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,825 bytes
コンパイル時間 1,303 ms
コンパイル使用メモリ 160,036 KB
実行使用メモリ 10,516 KB
最終ジャッジ日時 2024-04-19 05:55:59
合計ジャッジ時間 2,805 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
10,476 KB
testcase_01 AC 20 ms
10,500 KB
testcase_02 AC 20 ms
10,372 KB
testcase_03 AC 20 ms
10,364 KB
testcase_04 AC 21 ms
10,496 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 19 ms
10,400 KB
testcase_07 AC 20 ms
10,340 KB
testcase_08 AC 21 ms
10,444 KB
testcase_09 AC 86 ms
10,456 KB
testcase_10 AC 85 ms
10,420 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 21 ms
10,348 KB
testcase_13 AC 84 ms
10,340 KB
testcase_14 AC 70 ms
10,424 KB
testcase_15 AC 76 ms
10,440 KB
testcase_16 AC 53 ms
10,356 KB
testcase_17 AC 60 ms
10,516 KB
testcase_18 AC 46 ms
10,368 KB
testcase_19 AC 47 ms
10,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,bg,ed) for(ll i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007
#define int long long
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
const int INF = 1e9;

const int MAX_P = 909090;
ll fact[MAX_P];

void init_fact(int n, int mod)
{
    fact[0] = 0;
    fact[1] = 1;
    for (ll i=2; i<=n; i++) {
        fact[i] = (fact[i-1] * i) % mod;
    }
}

int mod_fact(int n, int p, int& e)
{
    e = 0;
    if (n == 0) return 1;

    int res = mod_fact(n / p, p, e);
    e += n / p;

    if (n / p % 2 != 0) return res * (p - fact[n % p]) % p;
    return res * fact[n % p] % p;
}

int extgcd(int a, int b, int& x, int& y)
{
    int d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}

int mod_inverse(int a, int m)
{
    int x, y;
    extgcd(a, m, x, y);
    return (m + x % m) % m;
}

int mod_comb(int n, int k, int p)
{
    if (n < 0 || k < 0 || n < k) return 0;
    int e1, e2, e3;
    int a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2), a3 = mod_fact(n - k, p, e3);
    if (e1 > e2 + e3) return 0;
    return a1 * mod_inverse(a2 * a3 % p, p) % p;
}

ll mod_pow(ll x, ll n, ll mod)
{
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

signed main()
{
    int N, M;
    cin >> N >> M;
    
    if (N < M) {
        cout << 0 << endl;
        return 0;
    }

    init_fact(903030, MOD);

    int ans = 0;
    int m = 1;
    for (int k=0; k<=M-1; k++) {
        int t = mod_comb(M, k, MOD) * mod_pow(M - k, N, MOD) % MOD;
        ans += m * t; 
        ans = (ans + MOD) % MOD;
        m *= -1;
    }

    while (ans < 0) ans += MOD;
    cout << ans << endl;
}
0