結果

問題 No.2428 Returning Shuffle
ユーザー nono00
提出日時 2023-08-18 22:15:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,995 bytes
コンパイル時間 2,810 ms
コンパイル使用メモリ 251,420 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-11-28 07:50:00
合計ジャッジ時間 5,213 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

namespace nono {

constexpr long long mod = 998244353;

long long pow(long long base, long long exp, long long mod) {
    long long result = 1;
    while (exp > 0) {
        if (exp & 1) {
            result *= base;
            result %= mod;
        }
        base *= base;
        base %= mod;
        exp >>= 1;
    }
    return result;
}

void solve() {
    int n, m;
    std::cin >> n >> m;
    std::vector<int> a(n);
    std::iota(a.begin(), a.end(), 0);
    for (int i = 0; i < m; i++) {
        int t;
        std::cin >> t;
        std::vector<int> s(t);
        for (int j = 0; j < t; j++) {
            std::cin >> s[j];
            s[j]--;
        }
        int temp = a[s.back()];
        for (int j = t - 1; j > 0; j--) {
            a[s[j]] = a[s[j - 1]];
        }
        a[s.front()] = temp;
    }

    //  for (int i = 0; i < n; i++) {
    //      std::cout << a[i] << ' ';
    //  }
    //  std::cout << std::endl;

    std::vector<bool> used(n);
    std::vector<int> cycle;
    for (int i = 0; i < n; i++) {
        if (not used[i]) {
            int s = i;
            int len = 0;
            while (not used[s]) {
                used[s] = true;
                s = a[s];
                len++;
            }
            if (len == 1) continue;
            cycle.push_back(len);
        }
    }
    long long ans = 1;
    long long gcd = 0;
    for (auto v: cycle) {
        ans *= v;
        ans %= mod;
        gcd = std::gcd(gcd, v);
    }
    //  std::cout << ans << ' ' << gcd << std::endl;
    if ((int)cycle.size() == 0) {
        std::cout << 1 << std::endl;
    } else if ((int)cycle.size() == 1) {
        std::cout << ans << std::endl;
    } else {
        std::cout << (ans * pow(gcd, mod - 2, mod) % mod) << std::endl;
    }
}

}  //  namespace nono

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(15);

    int t = 1;
    //  std::cin >> t;
    while (t--) nono::solve();
}
0