結果

問題 No.2428 Returning Shuffle
ユーザー ochiaigawaochiaigawa
提出日時 2023-08-18 23:01:22
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 1,677 bytes
コンパイル時間 2,469 ms
コンパイル使用メモリ 213,552 KB
実行使用メモリ 11,024 KB
最終ジャッジ日時 2024-11-28 09:35:24
合計ジャッジ時間 5,066 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace std;
using mint = atcoder::modint998244353;
int main() {
  cin.tie(0); cout.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> P(N);
  for(int i = 0; i < N; i++) {
    P[i] = i;
  }
  while(M--) {
    int T;
    cin >> T;
    vector<int> S(T);
    for(int i = 0; i < T; i++) {
      cin >> S[i];
      --S[i];
    }
    int fi = P[S.back()];
    for(int i = T - 1; i > 0; i--) {
      P[S[i]] = P[S[i - 1]];
    }
    P[S.front()] = fi;
    //for(int i = 0; i < N; i++) cout << P[i] << " \n"[i == N - 1];
  }
  bool ok = true;
  for(int i = 0; i < N; i++) {
    if(P[i] != i) {
      ok = false;
    }
  }
  if(ok) {
    cout << 1 << '\n';
  } else {
    vector<bool> dist(N, false);
    map<int, int> mp;
    for(int i = 0; i < N; i++) {
      if(!dist[i]) {
        int now = i;
        int cnt = 0;
        while(!dist[now]) {
          dist[now] = true;
          now = P[now];
          cnt++;
        }
        if(cnt > 1) {
          int T = cnt;
          for(int j = 2; j * j <= cnt; j++) {
            if(T % j == 0) {
              int c = 0;
              while(T % j == 0) {
                c++;
                T /= j;
              }
              if(mp.count(j) == 0) {
                mp[j] = c;
              } else {
                mp[j] = max(mp[j], c);
              }
            }
          }
          if(T > 1) {
            if(mp.count(T) == 0) {
              mp[T] = 1;
            }
          } 
        }
      }
    }
    mint ans = 1;
    for(auto [p, q] : mp) {
      ans *= mint(p).pow(q);
    }
    cout << ans.val() << '\n';
  }
  return 0;
}
0