結果
| 問題 |
No.2428 Returning Shuffle
|
| コンテスト | |
| ユーザー |
Manuel1024
|
| 提出日時 | 2023-08-18 22:07:14 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 505 ms / 2,000 ms |
| コード長 | 2,178 bytes |
| コンパイル時間 | 798 ms |
| コンパイル使用メモリ | 78,820 KB |
| 実行使用メモリ | 54,016 KB |
| 最終ジャッジ日時 | 2024-11-28 07:22:12 |
| 合計ジャッジ時間 | 5,834 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 23 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;
struct UnionFind{
vector<int> p;
UnionFind(int n): p(vector<int>(n, -1)){}
int root(int x){
if(p[x] < 0) return x;
else return p[x] = root(p[x]);
}
int size(int x){
return -p[root(x)];
}
void unite(int x, int y){
x = root(x);
y = root(y);
if(x == y) return;
if(size(x) < size(y)) swap(x, y);
p[x] += p[y];
p[y] = x;
}
};
ll mygcd(ll x, ll y){
while(y != 0){
ll r = x%y;
x = y; y = r;
}
return x;
}
int main(){
int n, m; cin >> n >> m;
vector<vector<int>> s(m);
for(int i = 0; i < m; i++){
int t; cin >> t;
s[i] = vector<int>(t);
for(auto &it: s[i]){
cin >> it;
it--;
}
}
vector<int> p(n);
for(int i = 0; i < n; i++){
p[i] = i;
}
for(const auto &v: s){
int x = p[v.back()];
for(int i = v.size()-2; i >= 0; i--){
p[v[i+1]] = p[v[i]];
}
p[v[0]] = x;
}
// for(int i = 0; i < n; i++) cerr << p[i] << " \n"[i == n-1];
UnionFind uf(n);
for(int i = 0; i < n; i++) uf.unite(i, p[i]);
const int lim = 2000000;
vector<int> isprime(lim);
for(int i = 0; i < lim; i++) isprime[i] = i;
for(int i = 2; i < lim; i++){
if(isprime[i] != i) continue;
for(int j = i+i; j < lim; j += i) isprime[j] = min(isprime[j], i);
}
vector<int> cnt(lim, 0);
for(int i = 0; i < n; i++){
if(uf.root(i) == i){
int x = uf.size(i);
// cerr << x << " ";
while(x > 1){
int y = isprime[x];
int z = 0;
while(x%y == 0){
z++;
x /= y;
}
cnt[y] = max(cnt[y], z);
}
}
}
ll ans = 1;
for(int i = 1; i < lim; i++){
for(int j = 0; j < cnt[i]; j++){
ans *= i;
ans %= MOD;
}
}
cout << ans << endl;
return 0;
}
Manuel1024