QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#206808#4937. Permutation TransformationmaghrabyJr_TL 0ms3576kbC++201.8kb2023-10-07 22:57:372023-10-07 22:57:38

Judging History

你现在查看的是最新测评结果

  • [2023-10-07 22:57:38]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3576kb
  • [2023-10-07 22:57:37]
  • 提交

answer

#include "bits/stdc++.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
#define int long long

const int MOD = 998244353;
int get(int x){
         if(x == 1) return 1;
         for(int p= 1, j= 2; ; j = j * 2 % x, p++){
                  if(j == 1) return p;
         }
}
int32_t main(){
         cin.tie(0);
         cin.sync_with_stdio(0);

         int n; cin>>n;
         int p[n], vis[n];
         for(int i = 0; i < n; i++){
                  cin>>p[i]; --p[i];
                  ::memset(vis, 0, sizeof vis);
         }
         vector<int> powers(n + 1, 0);
         int mx_tail= 0;
         for(int i = 0; i < n; i++){
                  if(vis[i]) continue;
                  int cur= i, sz= 0;
                  while(!vis[cur]){
                           vis[cur]= 1;
                           sz++;
                           cur= p[cur];
                  }
                  int tail= 0;
                  while(sz % 2 == 0){
                           sz /= 2;
                           tail++;
                  }
                  mx_tail = max(mx_tail, tail);
                  int period= get(sz);
                  for(int j = 2; j <= period; j++){
                           int pw= 0;
                           while(period % j == 0){
                                    period /= j;
                                    pw++;
                           }
                           powers[j]= max(powers[j], pw);
                  }
         }

         int LCM = 1;
         for(int i = 2; i <= n; i++){
                  while(powers[i]--){
                           LCM= LCM * i % MOD;
                  }
         }
         cout<<(LCM + mx_tail) % MOD;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3576kb

input:

5
3 5 1 2 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

8
7 5 1 6 8 2 3 4

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

1
1

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Time Limit Exceeded

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:


result: