QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404530 | #4937. Permutation Transformation | kevinyang# | WA | 16ms | 7836kb | C++17 | 1.6kb | 2024-05-04 04:25:40 | 2024-05-04 04:25:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct DisjointSet{
vector<int>parent,sz;
int size;
void init(int n){
size = n;
parent.resize(n+1); sz.resize(n+1);
for(int i = 1; i<=n; i++){
parent[i] = i;
sz[i] = 1;
}
}
int find(int x){
if(parent[x]==x)return x;
return find(parent[x]);
}
void Union(int x, int y){
x = find(x); y = find(y);
if(x==y)return;
if(sz[x]<sz[y]){
parent[x] = y;
sz[y]+=sz[x];
}
else{
parent[y] = x;
sz[x]+=sz[y];
}
}
};
const int mod = 998244353;
const int mxn = 100005;
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int>a(n+1);
for(int i = 1; i<=n; i++){
cin >> a[i];
}
DisjointSet ds;
ds.init(n+1);
for(int i = 1; i<=n; i++){
ds.Union(a[i],i);
}
set<int>s;
for(int i = 1; i<=n; i++){
s.insert(ds.find(i));
}
vector<int>factor(mxn);
vector<int>mx(mxn);
for(int i = 2; i<mxn; i++){
for(int j = i; j<mxn; j+=i){
if(!factor[j])factor[j] = i;
mx[j] = i;
}
}
vector<int>vals;
int extra = 0;
vector<int>dp(mxn);
for(int nxt: s){
map<int,int>hm;
int k = ds.sz[nxt];
if(k==1){
continue;
}
if(mx[k] == 2){
int cnt = 0;
while(k>1){
k/=2;
cnt++;
}
extra = max(extra,cnt);
}
else{
k--;
while(k>1){
hm[factor[k]]++;
k/=factor[k];
}
for(auto [a,b]: hm){
dp[a] = max(dp[a],b);
}
}
}
int ans = 1;
for(int i = 1; i<mxn; i++){
for(int j = 0; j<dp[i]; j++){
ans*=i;
ans%=mod;
}
}
ans+=extra;
ans%=mod;
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 5468kb
input:
5 3 5 1 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 5360kb
input:
8 7 5 1 6 8 2 3 4
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 4ms
memory: 5416kb
input:
1 1
output:
1
result:
ok single line: '1'
Test #4:
score: -100
Wrong Answer
time: 16ms
memory: 7836kb
input:
100000 20864 34918 58550 1465 75674 30743 27235 88900 47488 50029 46054 84871 20330 72228 16506 44561 92519 97750 82891 60324 90508 39290 24663 38077 90189 30671 95476 64027 70888 90749 22566 8525 33675 16635 23392 97636 35788 89625 41966 78051 94034 15407 26545 83799 2233 10873 56946 71566 19045 44...
output:
338374676
result:
wrong answer 1st lines differ - expected: '216333199', found: '338374676'