結果

問題 No.2428 Returning Shuffle
ユーザー planesplanes
提出日時 2023-08-18 21:54:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,008 ms / 2,000 ms
コード長 3,389 bytes
コンパイル時間 2,372 ms
コンパイル使用メモリ 182,760 KB
実行使用メモリ 141,180 KB
最終ジャッジ日時 2024-05-06 03:55:24
合計ジャッジ時間 25,639 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 856 ms
137,352 KB
testcase_01 AC 971 ms
141,180 KB
testcase_02 AC 996 ms
141,176 KB
testcase_03 AC 761 ms
105,944 KB
testcase_04 AC 770 ms
105,824 KB
testcase_05 AC 773 ms
105,944 KB
testcase_06 AC 778 ms
105,948 KB
testcase_07 AC 774 ms
105,952 KB
testcase_08 AC 764 ms
105,948 KB
testcase_09 AC 751 ms
105,948 KB
testcase_10 AC 763 ms
105,948 KB
testcase_11 AC 762 ms
105,820 KB
testcase_12 AC 753 ms
105,944 KB
testcase_13 AC 751 ms
105,816 KB
testcase_14 AC 779 ms
105,948 KB
testcase_15 AC 771 ms
105,948 KB
testcase_16 AC 783 ms
105,948 KB
testcase_17 AC 780 ms
105,948 KB
testcase_18 AC 761 ms
105,948 KB
testcase_19 AC 941 ms
140,188 KB
testcase_20 AC 949 ms
140,184 KB
testcase_21 AC 754 ms
105,944 KB
testcase_22 AC 759 ms
105,948 KB
testcase_23 AC 752 ms
105,816 KB
testcase_24 AC 1,008 ms
137,192 KB
testcase_25 AC 993 ms
137,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

  #include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)

ll INF=2e18;

struct UnionFind {

vector<pair<ll,ll>> par;
vector<ll> r;

void init(ll n) {
    par=vector<pair<ll,ll>> (n);
    r=vector<ll> (n);

    for(ll i=0;i<n;i++) {
        par[i]=make_pair(i,1);
        r[i]=0LL;
    }
}

ll find(ll x) {
    if(par[x].first==x) {
        return x;
    }
    return par[x].first=find(par[x].first);

}

void unite(ll x,ll y) {
    x=find(x);
    y=find(y);
    if(x==y) {
        return;
    }
    if(r[x]<r[y]) {
        par[x].first=y;
        par[y].second+=par[x].second;
    }
    else {
        par[y].first=x;
        par[x].second+=par[y].second;
    if(r[x]==r[y]) {
        r[x]++;
    }
    }
}

bool same(ll x,ll y) {
    return find(x)==find(y);
}

};


const int mod = 998244353;
class mint {
    long long x;
public:
    mint(long long x=0) : x((x%mod+mod)%mod) {}
    mint operator-() const { 
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= mod;
        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;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }

    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
};




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

ll N,M;cin>>N>>M;
vector<ll> P(N+1);
for(ll i=1;i<=N;i++) P[i]=i;

for(ll i=0;i<M;i++) {
  ll T;cin>>T;
  vector<ll> S(T);
  for(ll j=0;j<T;j++) cin>>S[j];

  ll memo=P[S[T-1]];
  for(ll j=T-1;j>=1;j--) {
    P[S[j]]=P[S[j-1]];
  }
  P[S[0]]=memo;
}






UnionFind uf;
uf.init(N+1);
for(ll i=1;i<=N;i++) {
  uf.unite(P[i],i);
}

vector<bool> note(N+1);
vector<ll> vec(0);

for(ll i=1;i<=N;i++) {
  ll k=uf.find(i);
  if(!note[k]) {
    note[k]=true;
    vec.push_back(uf.par[k].second);
  }
}


vector<ll> prime(0);
vector<bool> memo(1000000+100);
for(ll i=2;i<=1000000;i++) {
  if(!memo[i]) {
    prime.push_back(i);
  for(ll j=1;j*i<=1000000;j++) memo[j*i]=true;
  }
}



vector<vector<pair<ll,ll>>> d(1000000+100,vector<pair<ll,ll>> (0));

for(auto x:prime) {
  for(ll j=1;j*x<=1000000;j++) {
    ll count=0;
    ll now=j*x;
    while(now%x==0) {
      now/=x;
      count++;
    }
    d[j*x].push_back(make_pair(x,count));
  }
}

mint ans=1;
vector<ll> ma(1000000+10);
for(ll i=0;i<vec.size();i++) {
  for(auto x:d[vec[i]]) {
    while(ma[x.first]<x.second) {
      ma[x.first]++;
      ans*=x.first;
    }
  }
}

cout<<ans<<endl;
}







0