結果

問題 No.391 CODING WAR
ユーザー hiyokko2hiyokko2
提出日時 2017-07-30 16:25:14
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,690 bytes
コンパイル時間 1,345 ms
コンパイル使用メモリ 160,236 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-19 05:55:34
合計ジャッジ時間 6,250 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

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 = 403030;
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;

    init_fact(903030, MOD);

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

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