QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#133763#4937. Permutation Transformationckz#WA 7ms7740kbC++203.8kb2023-08-02 13:56:102023-08-02 13:56:14

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 13:56:14]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:7740kb
  • [2023-08-02 13:56:10]
  • 提交

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]--;
    // iota(all(a), 1);
    // a.back() = 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]);

    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)];

        // cerr << s << '\n';
        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;
    }

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

    // cerr << mxtail << '\n';
    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)];    
        int tmp = s / 2;
        int res = 1;
        for(int i = 1; i <= tmp; i++) res = res * 2 % (2 * tmp + 1);
        SZ[s] = res;
        // vector<int> v(s);
        // iota(all(v), 1);
        // v[s - 1] = 0;

        // cerr << s << '\n';

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


    // cerr << "???";

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

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3532kb

input:

5
3 5 1 2 4

output:

3

result:

ok single line: '3'

Test #2:

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

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: 3520kb

input:

1
1

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Wrong Answer
time: 7ms
memory: 7740kb

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:

259285318

result:

wrong answer 1st lines differ - expected: '216333199', found: '259285318'