結果

問題 No.1392 Don't be together
ユーザー merom686merom686
提出日時 2021-02-13 14:59:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,947 bytes
コンパイル時間 918 ms
コンパイル使用メモリ 90,120 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 23:26:44
合計ジャッジ時間 3,459 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 31 ms
4,384 KB
testcase_07 AC 92 ms
4,376 KB
testcase_08 AC 70 ms
4,376 KB
testcase_09 AC 66 ms
4,380 KB
testcase_10 AC 56 ms
4,380 KB
testcase_11 AC 85 ms
4,380 KB
testcase_12 AC 51 ms
4,376 KB
testcase_13 AC 73 ms
4,380 KB
testcase_14 AC 79 ms
4,376 KB
testcase_15 AC 54 ms
4,376 KB
testcase_16 AC 88 ms
4,380 KB
testcase_17 AC 58 ms
4,376 KB
testcase_18 AC 78 ms
4,376 KB
testcase_19 AC 69 ms
4,380 KB
testcase_20 AC 30 ms
4,380 KB
testcase_21 AC 13 ms
4,380 KB
testcase_22 AC 47 ms
4,380 KB
testcase_23 AC 32 ms
4,380 KB
testcase_24 AC 20 ms
4,380 KB
testcase_25 AC 36 ms
4,384 KB
testcase_26 AC 10 ms
4,380 KB
testcase_27 AC 15 ms
4,380 KB
testcase_28 AC 39 ms
4,380 KB
testcase_29 AC 20 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

constexpr int P = 998244353;

ll powmod(ll n, ll k) {
    ll r = 1, t = n % P;
    for (; k != 0; k /= 2) {
        if (k & 1) r = r * t % P;
        t = t * t % P;
    }
    return r;
}
ll modinv(ll n) {
    return powmod(n, P - 2);
}

vector<int> f0, f1;
void init(int n) {
    f0.resize(n + 1);
    f0[0] = 1;
    for (int i = 1; i <= n; i++) {
        f0[i] = (ll)f0[i - 1] * i % P;
    }
    f1.resize(n + 1);
    f1[n] = modinv(f0[n]);
    for (int i = n; i > 0; i--) {
        f1[i - 1] = (ll)f1[i] * i % P;
    }
}
ll fact(int k) {
    return f0[k];
}
ll comb(int n, int k) {
    if (n < k || k < 0) return 0;
    return (ll)f0[n] * f1[k] % P * f1[n - k] % P;
}

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

    int n, m;
    cin >> n >> m;

    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }

    vector<int> q(n, 0), u;
    for (int i = 0; i < n; i++) {
        if (q[i]) continue;
        int k = 1;
        q[i] = 1;
        for (int j = p[i]; j != i; j = p[j]) {
            k++;
            q[j] = 1;
        }
        u.push_back(k);
    }

    init(n);

    ll r = 0;
    for (int i = 1; i <= m; i++) {
        ll t = 1;
        for (int k : u) {
            ll dp[2] = {};
            dp[0] = i;
            for (int j = 0; j < k; j++) {
                ll d0 = dp[0];
                dp[0] = dp[1];
                dp[1] = (d0 * (i - 1) + dp[1] * max(i - 2, 0)) % P;
            }
            //cout << dp[0] << ' ' << powmod(i - 1, k) + (i - 1) * (k % 2 == 0 ? 1 : -1) << endl;
            t = t * dp[0] % P;
        }
        r += t * comb(m, i) % P * ((m - i) % 2 == 0 ? 1 : -1);
    }
    r %= P;
    if (r < 0) r += P;

    r = r * f1[m] % P;

    cout << r << endl;

    return 0;
}
0