QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133507 | #4937. Permutation Transformation | salvator_noster# | WA | 2ms | 4156kb | C++14 | 1.7kb | 2023-08-02 10:22:53 | 2023-08-02 10:22:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 100000 + 5;
using ll = long long;
const ll MOD = 998244353;
int n;
int li[SIZE];
vector<int> loops;
int vis[SIZE];
int primes[SIZE], pcnt;
int cntP[SIZE];
void init(void) {
static int mknp[SIZE];
for (int i = 2; i < SIZE; ++i) {
if (!mknp[i]) {
primes[++pcnt] = i;
for (int j = i << 1; j < SIZE; j += i) {
mknp[j] = 1;
}
}
}
return;
}
int main(void) {
init();
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &li[i]);
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
int len = 1;
vis[i] = 1;
for (int j = li[i]; j != i; j = li[j]) {
vis[j] = 1;
++len;
}
loops.push_back(len);
}
}
int pre = 0;
for (int len : loops) {
static int mk[SIZE];
for (int i = 0; i < len; ++i) {
mk[i] = 0;
}
int pow2 = 1;
int chain = -1;
for (int i = 1;; ++i) {
if (mk[pow2]) {
chain = i - mk[pow2];
pre = max(pre, mk[pow2] - 1);
// printf("pre = %d, chain = %d\n", mk[pow2] - 1, chain);
break;
}
mk[pow2] = i;
pow2 = pow2 * 2 % len;
}
for (int i = 1; primes[i] * primes[i] <= chain; ++i) {
if (chain % primes[i] == 0) {
int cnt = 0;
while (chain % primes[i] == 0) {
chain /= primes[i];
++cnt;
}
cntP[i] = max(cntP[i], cnt);
}
}
if (chain > 1) {
int id = lower_bound(primes + 1, primes + pcnt + 1, chain) - primes;
cntP[id] = max(cntP[id], 1);
}
}
ll prod = 1;
for (int i = 1; i <= pcnt; ++i) {
for (int j = 1; j <= cntP[i]; ++j) {
prod = prod * primes[i] % MOD;
}
}
ll ans = (prod + pre) % MOD;
printf("%lld\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 4092kb
input:
5 3 5 1 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 2ms
memory: 4068kb
input:
8 7 5 1 6 8 2 3 4
output:
4
result:
ok single line: '4'
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 4156kb
input:
1 1
output:
2
result:
wrong answer 1st lines differ - expected: '1', found: '2'