結果

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

テストケース

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

ソースコード

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(303030, 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