QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#130849 | #4937. Permutation Transformation | karuna# | WA | 34ms | 14544kb | C++17 | 2.4kb | 2023-07-25 12:50:26 | 2023-07-25 12:50:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const int MAXN = 101010;
int n, a[MAXN], ord[MAXN];
bool p[MAXN], dead[MAXN];
vector<int> divisor[MAXN];
ll _pow(ll a, int x, int mod) {
ll r = 1;
while (x) {
if (x & 1) r = r * a % mod;
a = a * a % mod;
x /= 2;
}
return r;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
p[0] = p[1] = 1;
vector<int> primes;
for (int i = 2; i < MAXN; i++) {
if (!p[i]) primes.push_back(i);
divisor[i].push_back(i);
for (int j = 2 * i; j < MAXN; j += i) {
divisor[j].push_back(i);
p[j] = 1;
}
}
for (int i = 0; i < MAXN; i++) p[i] ^= 1;
for (int p : primes) {
ll x = p;
while (x < MAXN) {
int phi = (x / p) * (p - 1);
for (int d : divisor[phi]) {
if (_pow(2, d, x) == 1) {
ord[x] = d;
break;
}
}
x = x * p;
}
}
int maxk = 0;
map<int, int> mp;
for (int i = 1; i <= n; i++) {
if (dead[i]) continue;
int cnt = 0;
for (int v = i; !dead[v]; v = a[v]) {
++cnt;
dead[v] = 1;
}
int k = 0;
while (cnt % 2 == 0) {
++k;
cnt /= 2;
}
maxk = max(maxk, k);
int x = cnt;
int period = 1;
for (int i = 3; i * i <= x; i++) {
int p = 1;
while (cnt % i == 0) {
cnt /= i;
p *= i;
}
period = (ll)period / __gcd(period, ord[p]) * ord[p];
}
if (cnt != 1) {
period = (ll)period / __gcd(period, ord[cnt]) * ord[cnt];
}
x = period;
for (int i = 2; i * i <= x; i++) {
int cnt = 0;
while (period % i == 0) {
period /= i;
++cnt;
}
mp[i] = max(mp[i], cnt);
}
if (period != 1) {
mp[period] = max(mp[period], 1);
}
}
ll ans = 1;
for (auto [x, y] : mp) {
while (y--) ans = ans * x % MOD;
}
cout << (ans + maxk) % MOD;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 29ms
memory: 14036kb
input:
5 3 5 1 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 31ms
memory: 14216kb
input:
8 7 5 1 6 8 2 3 4
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 34ms
memory: 14096kb
input:
1 1
output:
1
result:
ok single line: '1'
Test #4:
score: -100
Wrong Answer
time: 33ms
memory: 14544kb
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:
3
result:
wrong answer 1st lines differ - expected: '216333199', found: '3'