跳到正文

题解

CF2249D Xor Permutation Matrix 题解

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

文章信息

发布
2026年7月28日
收录
2026年8月7日
更新
2026年9月2日
篇幅
约 7 分钟
获赞
4
题目
CF2249D

版本

  1. v1当前
闲话

官解做法。

Hint 1

观察猜测怎么样的 n,xn,x 是有解的,大胆猜测当 nn 是 22 的幂次时才有解,我们考虑如何证明。

Hint 2

根据 xx 是否为 00 分类讨论,然后对于 xx 非 00 的情况我们考虑继续基于对换实现。

对应 Hint 1:

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

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

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

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

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

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

对应 Hint 2:

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

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

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

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

  • 对于偶数行那么它们分别是类似 pj⊕Sp_j\oplus S 的样子,肯定是排列。
  • 对于奇数行那么它们分别是类似 pj⊕((A⊕x)∪B)p_j\oplus((A\oplus x)\cup B) 的样子,实际上也是 pj⊕Sp_j\oplus S。

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

code
C++
#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;
}
原文