QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#209035 | #4937. Permutation Transformation | ZiadElGafy | TL | 0ms | 3808kb | C++20 | 1.9kb | 2023-10-10 04:34:02 | 2023-10-10 04:34:03 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define el '\n'
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef __int128 bigInt;
using namespace std;
const int N = 1e5 + 5, INF = 1e9 + 5, mod = 1e9 + 7, LOG = 21, SQ = 500;
int n, p[N], mx[N];
int solve(int cnt) {
if (cnt == 1) {
return 1;
}
int cur = 1, ret = 0;
do {
cur = (cur * 2) % cnt;
ret++;
} while (cur % cnt != 1);
return ret;
}
void doWork() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
vector<bool> vis(n + 1, 0);
int mxStart = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) {
continue;
}
vis[i] = 1;
int cnt = 1, cur = i;
while (!vis[p[cur]]) {
cnt++;
cur = p[cur];
vis[cur] = 1;
}
int curStart = 0;
while (cnt % 2 == 0) {
curStart++;
cnt >>= 1;
}
mxStart = max(mxStart, curStart);
int iterations = solve(cnt);
for (int j = 2; 1LL * j * j <= iterations; j++) {
int f = 0;
while (iterations % j == 0) {
f++;
iterations /= i;
}
mx[j] = max(mx[j], f);
}
if (iterations > 1) {
mx[iterations] = max(mx[iterations], 1);
}
}
int ans = 1;
for (int i = 2; i <= n; i++) {
while (mx[i]--) {
ans = 1LL * ans * i % mod;
}
}
ans = (ans + mxStart) % mod;
cout << ans << el;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tests = 1;
// cin >> tests;
for (int i = 1; i <= tests; i++) {
doWork();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
5 3 5 1 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3688kb
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: 3808kb
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...