QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#133691#4937. Permutation Transformationckz#TL 1ms3516kbC++203.4kb2023-08-02 12:58:332023-08-02 12:58:37

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-02 12:58:37]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3516kb
  • [2023-08-02 12:58:33]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()
using namespace std;

#define int ll
const int MOD = 998244353;
const int mod = 1e9 + 9;
const int p = 2333;
const int N = 1e5 + 10;

int n;
int fa[N], sz[N];

ll qpow(ll base, ll pow)
{
    ll res = 1;
    while(pow)
    {
        if(pow & 1) res = res * base % MOD;
        pow >>= 1;
        base = base * base % MOD;
    }
    return res;
}

int find(int x){
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y)
{
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(sz[x] > sz[y]) swap(x, y);
    fa[x] = y;
    sz[y] += sz[x];
}

void solve()
{
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i], a[i]--;

    for(int i = 0; i < n; i++) fa[i] = i, sz[i] = 1;
    for(int i = 0; i < n; i++) merge(i, a[i]);

    vector<int> vis(n);
    int mxtail = 0;
    for(int i = 0; i < n; i++)
    {
        if(vis[find(i)]) continue;
        vis[find(i)] = 1;

        int s = sz[find(i)];

        int tail = 0;
        while(s)
        {
            if(s % 2) break;
            s /= 2;
            tail++;
        }
        mxtail = max(mxtail, tail);

        // cerr << sz[find(i)] << ' ' << tail << '\n';
    }

    // cerr << mxtail << '\n';

    for(int i = 1; i <= mxtail; i++)
    {
        auto t = a;
        for(auto &it : t) it = a[it];
        a = t;
    }

    ///////////////////////////////////////////////////////////////////////////////////

    vector<int> SZ(n + 1);
    for(auto &it : vis) it = 0;
    for(int i = 0; i < n; i++) fa[i] = i, sz[i] = 1;
    for(int i = 0; i < n; i++) merge(i, a[i]);
    for(int i = 0; i < n; i++)
    {
        if(sz[find(i)] == 1 || SZ[sz[find(i)]]) continue;
        
        int s = sz[find(i)];        
        vector<int> v(s);
        iota(all(v), 1);
        v[s - 1] = 0;

        map<int, int> mp;
        int tot = 1;
        unsigned ll h = 0;
        for(auto &it : v) h = h * p + it;
        mp[h] = 1;
        while(1)
        {
            auto t = v;
            for(auto &it : t) it = v[it];
            v = t;
            h = 0;
            for(auto &it : v) h = h * p + it;

            if(mp[h])
            {
                int len = tot - mp[h] + 1;
                SZ[s] = len;
                break;
            }
            mp[h] = ++tot;
        }
    }

    vector<int> v;
    for(auto &it : SZ) if(it) v.push_back(it);

    if(v.empty()) cout << (mxtail + 1) % MOD;
    else if(v.size() == 1) cout << (mxtail + v[0]) % mod;
    else
    {
        vector<int> t(n + 1);
        for(auto it : v)
        {
            for(int i = 2; i * i <= it; i++)
            {
                if(it % i) continue;
                int res = 0;
                while(it % i == 0)
                {
                    res++;
                    it /= i;
                }
                t[i] = max(res, t[i]);
            }
            if(it > 1) t[it] = max(t[it], 1ll);
        }

        int ans = 1;
        for(int i = 2; i <= n; i++) if(t[i]) ans = ans * qpow(i, t[i]) % MOD;
        ans = (ans + mxtail) % MOD;

        cout << ans;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t = 1;
    //cin >> t;
    while(t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3516kb

input:

5
3 5 1 2 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3512kb

input:

8
7 5 1 6 8 2 3 4

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3460kb

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...

output:


result: