結果

問題 No.2428 Returning Shuffle
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-08-18 22:09:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 466 ms / 2,000 ms
コード長 4,260 bytes
コンパイル時間 3,789 ms
コンパイル使用メモリ 219,156 KB
実行使用メモリ 21,084 KB
最終ジャッジ日時 2024-05-06 04:14:14
合計ジャッジ時間 6,766 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 287 ms
18,944 KB
testcase_01 AC 451 ms
21,084 KB
testcase_02 AC 466 ms
21,048 KB
testcase_03 AC 2 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 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 332 ms
18,944 KB
testcase_20 AC 324 ms
18,944 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 327 ms
18,896 KB
testcase_25 AC 321 ms
18,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll modc = 998244353;

ll mod_exp(ll b, ll e, ll m){
    if (e > 0 && b == 0) return 0;
    ll ans = 1;
    b %= m;

    while (e > 0){
        if ((e & 1LL)) ans = (ans * b) % m;
        e = e >> 1LL;
        b = (b*b) % m;
    }

    return ans;
}

class mint {
    ll x;
public:
    mint(ll x=0) : x((x%modc+modc)%modc) {}
    mint operator-() const {
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += modc-a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= modc;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const {
        return pow(modc-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    bool operator == (const mint& a) const{
        return x == a.x;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    friend istream& operator>>(istream& ip, mint &m) {
        ll t;
        ip >> t;
        m = mint(t);
        return ip;
    }
    ll val(){
        return x;
    }
};

map<ll, ll> prime;

void prime_factor(ll n){
    ll m = n;

    if (n % 2 == 0){
        while(n % 2 == 0){
            prime[2]++;
            n /= 2;
        }
    }

    for (ll i = 3; i*i <= m; i+=2){
        if (n % i == 0){
            while(n % i == 0){
                prime[i]++;
                n /= i;
            }
        }
    }

    if (n != 1){
        prime[n]++;
    }
}

struct UnionFind {
    int ngroup, N;
    vector<int> par;
    vector<int> siz;

    UnionFind(int _N) : N(_N), par(_N), siz(_N) {
        ngroup = _N;
        for(int i = 0; i < N; i++){
            par[i] = i;
            siz[i] = 1;
        }
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    int unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return rx;
        ngroup--;
        if (siz[rx] > siz[ry]) swap(rx, ry);
        par[rx] = ry;
        siz[ry] += siz[rx];
        return ry;
    }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    int size(int x){
        return siz[root(x)];
    }

    int group_count(){
        return ngroup;
    }

    vector<vector<int>> groups(){
        vector<int> rev(N);
        int nrt=0;
        for (int i=0; i<N; i++){
            if (root(i) == i){
                rev[i] = nrt;
                nrt++;
            }
        }
        vector<vector<int>> res(nrt);
        for (int i=0; i<N; i++){
            res[rev[root(i)]].push_back(i);
        }

        return res;
    }
};

int main(){
 
    ll N, M, T, tmp;
    cin >> N >> M;
    vector<ll> P(N+1);
    for (int i=1; i<=N; i++) P[i] = i;
    for (int i=0; i<M; i++){
        cin >> T;
        vector<ll> S(T);
        for (int i=0; i<T; i++) cin >> S[i];
        tmp = P[S[T-1]];
        for (int i=T-2; i>=0; i--){
            P[S[i+1]] = P[S[i]];
        }
        P[S[0]] = tmp;
    }

    UnionFind tree(N+1);
    for (int i=1; i<=N; i++){
        tree.unite(P[i], i);
    }
    vector<ll> v;
    for (int i=1; i<=N; i++){
        if (tree.root(i) == i && tree.size(i) > 1)  v.push_back(tree.size(i));
    }

    mint ans=1;
    map<ll, ll> mp;
    for (auto x : v){
        prime.clear();
        prime_factor(x);
        for (auto [y, z] : prime) mp[y] = max(mp[y], z);
    }

    for (auto [x, y] : mp) ans *= mod_exp(x, y, modc);

    cout << ans << endl;

    return 0;
}
0