結果

問題 No.2428 Returning Shuffle
ユーザー tnakao0123tnakao0123
提出日時 2023-08-22 14:50:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 3,493 bytes
コンパイル時間 549 ms
コンパイル使用メモリ 58,304 KB
実行使用メモリ 16,808 KB
最終ジャッジ日時 2023-08-22 14:50:40
合計ジャッジ時間 5,104 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
11,648 KB
testcase_01 AC 276 ms
11,616 KB
testcase_02 AC 280 ms
11,664 KB
testcase_03 AC 7 ms
8,228 KB
testcase_04 AC 7 ms
8,184 KB
testcase_05 AC 7 ms
8,208 KB
testcase_06 AC 7 ms
8,168 KB
testcase_07 AC 7 ms
8,116 KB
testcase_08 AC 7 ms
8,172 KB
testcase_09 AC 7 ms
8,172 KB
testcase_10 AC 7 ms
8,216 KB
testcase_11 AC 7 ms
8,212 KB
testcase_12 AC 7 ms
8,232 KB
testcase_13 AC 7 ms
8,228 KB
testcase_14 AC 7 ms
8,168 KB
testcase_15 AC 7 ms
8,208 KB
testcase_16 AC 7 ms
8,232 KB
testcase_17 AC 8 ms
8,184 KB
testcase_18 AC 7 ms
8,236 KB
testcase_19 AC 154 ms
11,612 KB
testcase_20 AC 153 ms
11,608 KB
testcase_21 AC 8 ms
8,216 KB
testcase_22 AC 7 ms
8,176 KB
testcase_23 AC 7 ms
8,116 KB
testcase_24 AC 152 ms
16,772 KB
testcase_25 AC 151 ms
16,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2428.cc:  No.2428 Returning Shuffle - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 1000000;
const int MAX_P = 1000000;
const int MOD = 998244353;

/* typedef */

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

/* global variables */

bool primes[MAX_P + 1];
int ps[MAX_N], ss[MAX_N], qs[MAX_N];
bool used[MAX_N];

/* subroutines */

int gen_primes(int maxp, vi &pnums) {
  fill(primes, primes + maxp + 1, true);
  primes[0] = primes[1] = false;

  int p;
  for (p = 2; p * p <= maxp; p++)
    if (primes[p]) {
      pnums.push_back(p);
      for (int q = p * p; q <= maxp; q += p) primes[q] = false;
    }
  for (; p <= maxp; p++)
    if (primes[p]) pnums.push_back(p);
  return (int)pnums.size();
}

bool prime_decomp(int n, vi &pnums, vpii& pds) {
  pds.clear();

  int pn = pnums.size();
  for (int i = 0; i < pn; i++) {
    int pi = pnums[i];
    if (pi * pi > n) {
      if (n > 1) pds.push_back(pii(n, 1));
      return true;
    }

    if (n % pi == 0) {
      int fi = 0;
      while (n % pi == 0) n /= pi, fi++;
      pds.push_back(pii(pi, fi));
    }
  }
  return false;
}

/* main */

int main() {
  vi pnums;
  gen_primes(MAX_P, pnums);

  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < n; i++) ps[i] = i;

  for (int i = 0; i < m; i++) {
    int ti;
    scanf("%d", &ti);
    for (int j = 0; j < ti; j++) scanf("%d", ss + j), ss[j]--;

    for (int j = 0; j < ti; j++) qs[j] = ps[ss[j]];
    for (int j = 0; j < ti; j++) ps[ss[(j + 1) % ti]] = qs[j];
  }
  
  //for (int i = 0; i < n; i++) printf("%d ", ps[i]); putchar('\n');

  vpii lpds;

  for (int st = 0; st < n; st++)
    if (! used[st]) {
      int l = 0;
      for (int u = st; ! used[u]; u = ps[u])
	used[u] = true, l++;
      if (l > 1) {
	vpii pds, lpds1;
	prime_decomp(l, pnums, pds);

	int i = 0, j = 0, n0 = lpds.size(), n1 = pds.size();
	while (i < n0 && j < n1) {
	  if (lpds[i].first < pds[j].first)
	    lpds1.push_back(lpds[i++]);
	  else if (lpds[i].first > pds[j].first)
	    lpds1.push_back(pds[j++]);
	  else {
	    int e = max(lpds[i].second, pds[j].second);
	    lpds1.push_back(pii(lpds[i].first, e));
	    i++, j++;
	  }
	}
	while (i < n0)
	  lpds1.push_back(lpds[i++]);
	while (j < n1)
	  lpds1.push_back(pds[j++]);
	swap(lpds, lpds1);
      }
    }

  mi p = 1;
  for (auto &pd: lpds)
    p *= mi(pd.first).pow(pd.second);

  printf("%d\n", p.v);
  return 0;
}
0