QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133489 | #4937. Permutation Transformation | Delay_for_five_minutes# | RE | 2ms | 5844kb | C++14 | 2.1kb | 2023-08-02 10:04:37 | 2023-08-02 10:04:44 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n;
const int mod = 998244353;
int p[100005];
bool vis[100005];
vector<int> v;
int phi[100005];
vector<int> d[100005];
int fpow(int a,int b,int m)
{
int ans = 1;
while(b){
if(b & 1) ans = 1LL*ans * a % mod;
a = 1LL*a*a%m;b >>= 1;
}
return ans;
}
int get(int x)
{
if(x == 1) return 1;
int u = phi[x];
// printf("X %d , %d\n",x,u);
for(auto y : d[u]) {
if(fpow(2 , y , x) == 1) return y;
}
assert(0);
return -1;
}
vector<int> pr;
map<int,int> ans;
void frac(int x)
{
for(auto y : pr) {
if(y > x) break;
int d = 0;
while(x % y == 0) {
x /= y ; d++;
}
if(d) ans[y] = max(ans[y] , d);
}
if(x > 1) ans[x] = max(ans[x] , 1);
return;
}
int main()
{
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++) phi[i] = i;
for(int i = 2;i <= n;i++) {
if(phi[i] == i) {
for(int j = i ;j <= n ;j += i) phi[j] = phi[j] / i * (i - 1);
if(1LL*i*i <= n) pr.push_back(i);
}
}
for(int i = 1;i <= n;i++) {
for(int j = i;j <= n;j += i) d[j].push_back(i);
}
for(int i = 1;i <= n;i++) cin >> p[i];
for(int i = 1;i <= n;i++) {
if(!vis[i]) {
int u = i;
int cnt = 0;
while(!vis[u]) {
vis[u] = 1;u = p[u]; cnt++;
}
v.push_back(cnt);
}
}
int cc = 0;
set<int> rv;
for(auto x : v) {
int cnt = 1 , np = x , rc = 0;
while(x % 2 == 0) {
x >>= 1 ; cnt *= 2;rc++;
}
// printf("Xx %d\n",x);
rv.insert(x);
cc = max(cc , rc) ;
}
for(auto x : rv) {
int rk = get(x);
frac(rk);
}
int sol = 1;
for(auto &[x,y] : ans) sol = 1LL*sol*fpow(x , y , mod) % mod;
sol = (sol + cc) % mod;
cout << sol;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5832kb
input:
5 3 5 1 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 5836kb
input:
8 7 5 1 6 8 2 3 4
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 2ms
memory: 5844kb
input:
1 1
output:
1
result:
ok single line: '1'
Test #4:
score: -100
Dangerous Syscalls
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...