CF2249D Xor Permutation Matrix 题解

· · 题解

:::::info[闲话] 官解做法。 ::::: :::::success[Hint 1] 观察猜测怎么样的 n,x 是有解的,大胆猜测当 n 是 2 的幂次时才有解,我们考虑如何证明。 ::::: :::::success[Hint 2] 根据 x 是否为 0 分类讨论,然后对于 x 非 0 的情况我们考虑继续基于对换实现。 :::::

对应 Hint 1:

我们猜测怎样的 n,x 是有解的,我们感受到 n 不是 2 的幂次时不太能有解,我们考虑证明这一点。

我们考虑对每行进行异或差分,设 b_{i,j}=a_{i,j}\oplus a_{i,j+1},那么我们有 b_{i,j}\oplus b_{i+1,j}=x,那么至少有 \lceil\frac n2\rceil 个 i 的 b_{i,j} 是相同的,令 S=\{0,1,\cdots,n-1\} 那么 \sum_{i=0}^{n-1}[S\oplus i=S]\ge\lceil\frac n2\rceil。

打表观察到 S\oplus i=S\Leftrightarrow 0\le i<\text{lowbit}(n),考虑证明这一点。

令 L=\text{lowbit}(n),i=0 是平凡的,当 0<i<L 时我们设 S 中的一个元素为 d,然后我们设 d=pL+q(0\le q<L),那么 d\oplus i=pL+(q\oplus i),由于 q 和 q\oplus i 形成对换,所以 S\oplus i=S 仍然成立。

当 i\ge L 时,设 i=cL+d(0\le d<L),那么 d\oplus i=(p\oplus c)L+(q\oplus d),此时 p 大组中的元素和 p\oplus c 大组中的元素构成了对换,但是由于 \frac{n}{L} 是奇数,那么肯定不可能两两都形成对换。

那么我们证明了上面的命题,也就是说我们要求 L\ge\frac n2,即 \frac nL\le 2,由于它是奇数那么 n=L 一定成立,即 n 是 2 的幂次。

对应 Hint 2:

我们考虑如何构造,尝试按照 x 是否为 0 分类讨论。

当 x=0 时,我们尝试一些朴素的构造,我们发现令 a_{i,j}=i\oplus j 符合条件,x=0 的情况就解决了。

当 x\ne 0 时,我们可以对于所有横纵下标都是奇数的位置都异或上 x,但是这样可能会影响排列的要求,我们考虑怎么办。

我们考虑到 d 和 d\oplus x 形成了对换,那么我们将 d\oplus x 和 d 打包成为一组,然后我们将其分为 A 和 B 两大组,每大组有 \frac n4 组对换(因此我们还需要特殊讨论 n=2 的情况),然后我们将 A 和 B 中的元素交替放入一个序列 \{p_n\} 中,那么令 a_{i,j}=p_i\oplus p_j\oplus(i\bmod 2)(j\bmod 2)x,我们考虑证明这样是对的。

这样我们就做完了这道题。
时间复杂度为 \Theta(n^2),空间复杂度为 \Theta(n)。

:::::success[code]

#include<bits/stdc++.h>
#define int long long
#define inl inline
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i) 
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i) 
#define endl '\n'
#define INF 1e9 
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front 
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int> 
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream 
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=2505;
bool vis[N];
int p[N];
inl void solve(){
    int n,x;
    cin>>n>>x;
    if(n&n-1){
        cout<<-1<<endl;
        return;
    }
    if(n==2){
        if(!x){
            cout<<0<<' '<<1<<endl<<1<<' '<<0<<endl;
            return; 
        }
        cout<<-1<<endl;
        return;
    }
    if(!x){
        rep(i,0,n-1){
            rep(j,0,n-1) cout<<(i^j)<<' ';
            cout<<endl;
        }
        return;
    }
    vector<int>a,b;
    rep(i,0,n-1) vis[i]=0;
    rep(i,0,n-1)
        if(!vis[i]){
            vis[i]=vis[i^x]=1;
            if((int)a.size()<n>>1){
                a.pb(i);
                a.pb(i^x);
            }else{
                b.pb(i);
                b.pb(i^x);
            }
        }
    rep(i,1,n)
        if(i&1) p[i]=a[i>>1];
        else p[i]=b[i-1>>1];
    rep(i,1,n){
        rep(j,1,n)
            if((i&1)&&(j&1)) cout<<(p[i]^p[j]^x)<<' ';
            else cout<<(p[i]^p[j])<<' ';
        cout<<endl; 
    }
}
si main(){
    fst;
    int t;
    cin>>t;
    while(t--) solve();
    return 0;
}

:::::