結果

問題 No.391 CODING WAR
ユーザー sahiyasahiya
提出日時 2020-12-29 19:33:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 2,183 bytes
コンパイル時間 1,454 ms
コンパイル使用メモリ 113,788 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 19:37:21
合計ジャッジ時間 2,354 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 22 ms
5,376 KB
testcase_10 AC 21 ms
5,376 KB
testcase_11 AC 12 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 19 ms
5,376 KB
testcase_14 AC 17 ms
5,376 KB
testcase_15 AC 18 ms
5,376 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 10 ms
5,376 KB
testcase_19 AC 10 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef bitset<16> BS;
struct edge {
    int to, cost, id;
};
const ll MOD = 1E+09 + 7; // =998244353;
const ll INF = 1E18;
const int MAX_N = 1E+05;

ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

ll N, M;

ll modfac[MAX_N + 1];
ll modfacinv[MAX_N + 1];

ll modPow(ll x, ll a);
ll modfactor(ll p);
void modfactorInv(ll n);

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> M;
    modfactor(M);

    ll ans = 0;
    for (ll i = 0; i <= M; i++) {
        ll res = ((modfac[M] * modfacinv[M - i]) % MOD * modfacinv[i]) % MOD;
        res *= modPow(M - i, N);
        res %= MOD;
        if (i & 1) {
            ans += (-res + MOD) % MOD;
            ans %= MOD;
        } else {
            ans += res;
            ans %= MOD;
        }
    }

    /*
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
        }
    }
    */

    cout << ans << "\n";

    return 0;
}

// n!を求める
ll modfactor(ll n)
{
    modfac[0] = 1;
    ll p = 1;
    while (p <= n) {
        //cout << p << "\n";
        modfac[p] = (p * modfac[p - 1]) % MOD;
        p++;
    }
    modfacinv[n] = modPow(modfac[n], MOD - 2);
    modfactorInv(n);
    return modfac[n];
}

// (n!)^-1を求める
void modfactorInv(ll n)
{
    ll p = n - 1;
    while (p >= 0) {
        modfacinv[p] = ((p + 1) * modfacinv[p + 1]) % MOD;
        p--;
    }
}

ll modPow(ll x, ll a)
{
    ll ans = 1;
    while (a > 0) {
        if (a & 1) {
            ans = (ans * x) % MOD;
        }
        x = (x * x) % MOD;
        a >>= 1;
    }
    return ans;
}
0