QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#767367#4937. Permutation TransformationNYCU_template#RE 1ms3848kbC++201.4kb2024-11-20 20:43:312024-11-20 20:43:36

Judging History

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

  • [2024-11-20 20:43:36]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3848kb
  • [2024-11-20 20:43:31]
  • 提交

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";
}

Details

Tip: Click on the bar to expand more detailed information

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

output:


result: