跳到正文

题解

题解:P10818 [EC Final 2020] Random Shuffle

_Ch1F4N_约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟
题目
P10818

版本

  1. v1当前

模拟赛场切了,开心。

容易还原出每一轮生成的随机数模 ii 后的值,不妨记为 toi mod ito_i \bmod i。

考虑怎么通过 toi mod ito_i \bmod i 还原出最开始的 xx。

由于是通过二进制操作生成的 toito_i,所以 toito_i 在二进制下每一位一定都是 xx 二进制下一些位异或的结果,先处理出来,然后考虑怎么通过 toi mod ito_i \bmod i 获取 toito_i 的二进制位的值。

首先 toi mod 2kto_i \bmod 2^k 就是 toito_i 二进制下第 [0,k)[0,k) 位的值,但是只知道这些信息远远不够。

注意到 toi mod (2k×l) mod 2k=toi mod 2kto_i \bmod (2^k \times l) \bmod 2^k = to_i \bmod 2^k,结合这一部分的信息后,当 n=50n = 50 时对得到的所有方程做高斯消元后,你发现存在 1717 个位置没有确定,于是暴力枚举这 1717 个位置的值,check 就是把跑出来的 xx 对前 5050 个位置做一下,如果这部分合法则很大概率则为合法解。

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+114;
bitset<64> bit[maxn][64];
bitset<64> now;
vector< bitset<64> > vec;
int cnt[maxn];
bitset<64> v[64],ans[64];
#define ull unsigned long long
ull w[64];
ull jump(ull x){
    x^=x<<13;
    x^=x>>7;
    x^=x<<17;
    return x;
}
int ChiFAN[17]={63,62,61,60,59,58,57,56,55,54,53,52,51,50,48,47,45};
//0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
void init(){
    int n=50;
    for(int i=0;i<64;i++) bit[0][i][i]=1;
    for(int i=0;i<17;i++){
        bitset<64> b;
        b[ChiFAN[i]]=1;
        vec.push_back(b);
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<64;j++){
            bit[i][j]=bit[i-1][j];
        }
        for(int j=63;j>=13;j--) bit[i][j]^=bit[i][j-13];
        for(int j=0;j+7<64;j++) bit[i][j]^=bit[i][j+7];
        for(int j=63;j>=17;j--) bit[i][j]^=bit[i][j-17];
        int lowbit=(i&(-i));
        for(int j=0,p=0;(1<<j)<lowbit;j++,p++){
            vec.push_back(bit[i][p]);
            now|=bit[i][p];
            cnt[i]++;
            if(vec.size()==64&&now.count()==64) break;
        }
        if(vec.size()==64&&now.count()==64) break;
    }
    for(int i=0;i<64;i++) v[i][i]=1;
    for(int i=0;i<64;i++){
        for(int j=i;j<64;j++){
            if(vec[j][i]==1){
                swap(vec[j],vec[i]);
                swap(v[j],v[i]);
                break;
            }
        }
        for(int j=i+1;j<64;j++){
            if(vec[j][i]==1){
                vec[j]^=vec[i];
                v[j]^=v[i];
            }
        }
    }
    for(int i=63;i>=0;i--){
        ans[i]=v[i];
        for(int j=i+1;j<64;j++){
            if(vec[i][j]==1){
                ans[i]^=ans[j];
            }
        }
    }
    for(int i=0;i<64;i++){
        for(int j=0;j<64;j++){
            if(ans[i][j]==1) w[i]+=((ull)(1)<<j);
        }
    }
    return ;
}
int a[maxn],p[maxn],to[maxn];
const int warma = 50;
void work(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i],p[a[i]]=i;
    for(int i=n;i>=1;i--){
        to[i]=p[i];
        swap(a[i],a[to[i]]);
        swap(p[a[i]],p[a[to[i]]]);
    }
    for(int i=1;i<=n;i++) to[i]--;
    ull S=0;
    int chifan=0;
    for(int i=1;i<=50;i++){
        for(int j=0;j<cnt[i];j++){
            if(((ull)(1)<<j)&to[i]){
                S+=((ull)(1)<<chifan);
            }
            chifan++;
        }
    }
    for(ull v=0;v<(1<<17);v++){
        ull T=v+(S<<17);
        //__builtin_popcountll()
        ull x=0;
        for(int i=0;i<64;i++){
            if(__builtin_popcountll(T&w[i])%2==1) x+=((ull)(1)<<i);
        }
        ull y=x;
        int i=1;
        for(i=1;i<warma;i++){
            y=jump(y);
            if(y%i!=to[i]) break;
        }
        if(i==warma){
            cout<<x<<'\n';
            return ;
        }
    }
    return ;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    init();
    int T;
    T=1;
    while(T--) work();
    return 0;
}
/*
1
50
47 14 37 36 43 2 34 26 48 6 45 11 27 22 9 42 4 13 33 46 18 25 12 19 32 49 8 40 15 7 1 29 44 24 20 17 30 50 16 35 39 21 28 41 10 23 3 5 38 31
18446744073709551615
*/
原文