QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#133669#4937. Permutation Transformationckz#WA 961ms6180kbC++202.4kb2023-08-02 12:44:122023-08-02 12:44:13

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:44:13]
  • 评测
  • 测评结果:WA
  • 用时:961ms
  • 内存:6180kb
  • [2023-08-02 12:44:12]
  • 提交

answer

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

const int mod = 1e9 + 9;
const int p = 2333;
const int N = 1e5 + 10;

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

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]);
    int lc = 1;
    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;
                lc = lcm(lc, len);
                break;
            }
            mp[h] = ++tot;
        }
    }

    cout << lc + mxtail;
}

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

input:

5
3 5 1 2 4

output:

3

result:

ok single line: '3'

Test #2:

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

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

input:

1
1

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Wrong Answer
time: 961ms
memory: 6180kb

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:

195259547

result:

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