QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#767367 | #4937. Permutation Transformation | NYCU_template# | RE | 1ms | 3848kb | C++20 | 1.4kb | 2024-11-20 20:43:31 | 2024-11-20 20:43:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int N = 100005;
ll MOD = 998244353;
ll fp(ll x, ll y, ll m) {
ll res = 1;
while(y) {
if(y & 1) res = res * x % m;
y >>= 1;
x = x * x % m;
}
return res;
}
ll inv(ll x, ll p) {
return fp(x, p - 2, p);
}
int p[N];
int main() {
cin.tie(0)->sync_with_stdio(false);
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> p[i];
}
set<int> used;
vector<int> sz;
for(int i = 1; i <= n; i++) {
if(used.count(i)) continue;
int cur = i, len = 0;
while(!used.count(cur)) {
used.insert(cur);
len++;
cur = p[cur];
}
sz.push_back(len);
}
vector<ll> xs;
ll mx_a = 0;
for(auto s : sz) {
ll a = 0;
while(s % 2 == 0) {
a++;
s /= 2;
}
mx_a = max(mx_a, a);
for(ll x = 1; x <= s + 1; x++) {
if(fp(2, x, s) == 1) {
xs.push_back(x);
break;
}
}
}
ll gd = xs[0];
for(auto x : xs) {
gd = __gcd(gd, x);
}
ll lcm = gd;
for(auto x : xs) {
lcm *= (x * inv(gd, MOD));
lcm %= MOD;
}
cout << (lcm + mx_a) % MOD << "\n";
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3848kb
input:
5 3 5 1 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
8 7 5 1 6 8 2 3 4
output:
4
result:
ok single line: '4'
Test #3:
score: -100
Runtime Error
input:
1 1