跳到正文

题解

题解:P14939 「FAOI-R10」异或矩阵

qianyuzhe约 14 分钟
原文
文章信息与版本

文章信息

收录
2026年1月14日
更新
2026年2月10日
篇幅
约 14 分钟
题目
P14939

版本

  1. v1当前

好题。

设 a&ba\&b 表示 a,ba,b 按位与的结果。

a⊕b>b−a⇔a+b−2(a&b)>b−a⇔2a−2(a&b)>0⇔a≠a&b\begin{aligned}a\oplus b>b-a&\Leftrightarrow a+b-2(a\&b)>b-a\\&\Leftrightarrow 2a-2(a\&b)>0\\&\Leftrightarrow a\not=a\&b\end{aligned}

这对 a>ba>b 其实是一定成立的,因为 a>b≥a&ba>b\ge a\&b。

但是这对做出这题并非必要,因为赛时我也没注意到,不过在下面的证明中需要用到该结论。

我们先把 n=4n=4 的表打出来试试:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 1 0 1 0 1 0 1 0 1 0 1 0 1 00 0 0 0 1 1 0 0 1 1 0 0 1 1 0 00 0 0 0 1 1 1 0 1 1 1 0 1 1 1 00 0 0 0 0 0 0 0 1 1 1 1 0 0 0 00 0 0 0 0 0 1 0 1 1 1 1 1 0 1 00 0 0 0 0 0 0 0 1 1 1 1 1 1 0 00 0 0 0 0 0 0 0 1 1 1 1 1 1 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 1 0 1 0 1 00 0 0 0 0 0 0 0 0 0 0 0 1 1 0 00 0 0 0 0 0 0 0 0 0 0 0 1 1 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{orange}0\ \color{orange}0\ \color{orange}0\ \color{orange}0\ \color{orange}0\ \color{orange}0\ \color{orange}0\ \color{orange}0\\ \color{red}0\ \color{red}0\ \color{red}1\ \color{red}0\ \color{red}1\ \color{red}0\ \color{red}1\ \color{red}0\ \color{gold}1\ \color{orange}0\ \color{orange}1\ \color{orange}0\ \color{orange}1\ \color{orange}0\ \color{orange}1\ \color{orange}0\\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}1\ \color{red}1\ \color{red}0\ \color{red}0\ \color{gold}1\ \color{gold}1\ \color{orange}0\ \color{orange}0\ \color{orange}1\ \color{orange}1\ \color{orange}0\ \color{orange}0\\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}1\ \color{red}1\ \color{red}1\ \color{red}0\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{orange}0\ \color{orange}1\ \color{orange}1\ \color{orange}1\ \color{orange}0\\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{orange}0\ \color{orange}0\ \color{orange}0\ \color{orange}0\\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}1\ \color{red}0\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{orange}0\ \color{orange}1\ \color{orange}0\\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{orange}0\ \color{orange}0\\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{red}0\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{gold}1\ \color{orange}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}1\ \color{blue}0\ \color{blue}1\ \color{blue}0\ \color{blue}1\ \color{blue}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}1\ \color{blue}1\ \color{blue}0\ \color{blue}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}1\ \color{blue}1\ \color{blue}1\ \color{blue}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}1\ \color{blue}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{green}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0\ \color{blue}0

不难发现形成了一个递归的结构,所以考虑直接递归求解。

先考虑 11。设 fn,x,yf_{n,x,y} 表示 2n×2n2^n\times2^n 矩阵中 (x,y)(x,y) 所在四连通块中 11 的个数,对 n<2n<2 显然 fn,x,y=0f_{n,x,y}=0;对 n>1n>1,可以分为图示五部分求解,于是有:

fn,x,y={0,n=1∨x≥2n−1∧y<2n−1fn−1,x,y,x<2n−1∧y<2n−1fn−1,x−2n−1,y−2n−1,x≥2n−1∧y≥2n−14n−1−2n−1,x<2n−1∧y≥2n−1∧x>y−2n−1fn−1,x,y−2n−1,x<2n−1∧y≥2n−1∧x≤y−2n−1f_{n,x,y}=\begin{cases} \color{green}0,n=1\lor x\ge2^{n-1}\land y<2^{n-1}\\ \color{red}f_{n-1,x,y},x<2^{n-1}\land y<2^{n-1}\\ \color{blue}f_{n-1,x-2^{n-1},y-2^{n-1}},x\ge2^{n-1}\land y\ge2^{n-1}\\ \color{gold}4^{n-1}-2^{n-1},x<2^{n-1}\land y\ge2^{n-1}\land x>y-2^{n-1}\\ \color{orange}f_{n-1,x,y-2^{n-1}},x<2^{n-1}\land y\ge2^{n-1}\land x\le y-2^{n-1} \end{cases}

可是 nn 很大,怎么办呢?没关系,因为 x,y≤1018x,y\le10^{18},如果我们只关心 11 的情况,如果 nn 过大,直接把 nn 改为 ⌈log⁡2max⁡{x,y}⌉\lceil\log_2\max\{x,y\}\rceil 即可,时间复杂度 O(log⁡max⁡{x,y})O(\log\max\{x,y\})。

对递归结构的证明

考虑数学归纳法。

当 n<2n<2 显然成立。

当 n≥2n\ge2,分类讨论:

  • x≥2n−1,y<2n−1x\ge2^{n-1},y<2^{n-1}。因 x≥2n−1>yx\ge 2^{n-1}>y,故 fn,x,y=0f_{n,x,y}=0。
  • x<2n−1,y<2n−1x<2^{n-1},y<2^{n-1}。显然 fn,x,y=fn−1,x,yf_{n,x,y}=f_{n-1,x,y}。
  • x≥2n−1∧y≥2n−1x\ge2^{n-1}\land y\ge2^{n-1}。由于 x,yx,y 二进制下最高位均为 11,显然同时减去 2n−12^{n-1} 不改变 x⊕yx\oplus y 和 y−xy-x 的值,因此 fn,x,y=fn−1,x−2n−1,y−2n−1f_{n,x,y}=f_{n-1,x-2^{n-1},y-2^{n-1}}。
  • x<2n−1,y≥2n−1x<2^{n-1},y\ge2^{n-1}。令 y′=y−2n−1y'=y-2^{n-1},易知 xx 及 x&yx\&y 的值都不变,所以相当于 n′=n−1n'=n-1 时去除 y≥xy\ge x 条件的结果。

命题得证。

再考虑 00。判断一个位置是否是 00 是简单的,直接求一下 fn,x,yf_{n,x,y} 是否为 00 即可。(赛时犯唐了,别管这句话。)显然所有的 00 在一个连通块内,所以只需关注全局 00 的总数。设 gng_{n} 表示 2n×2n2^n\times2^n 矩阵中 00 的个数,根据递归结构易知 gn=3gn−1+4n−1−2n−1(2n−1−1)2=3gn−1+22n−3+2n−2g_n=3g_{n-1}+4^{n-1}-\dfrac{2^{n-1}(2^{n-1}-1)}{2}=3g_{n-1}+2^{2n-3}+2^{n-2},查阅 oeis 列出前几项(下标从 00 开始):

1,4,15,55,201,739,2745,10315,39201,150499,582825…1,4,15,55,201,739,2745,10315,39201,150499,582825\dots

一眼瞪出 gn=22n−1−2n−1+3ng_n=2^{2n-1}-2^{n-1}+3^n。

对通项公式的证明

考虑数学归纳法。

当 n=0n=0 显然成立。

当 n>0n>0,若:

gn−1=22(n−1)−1−2(n−1)−1+3n−1=22n−3−2n−2+3n−1\begin{aligned}g_{n-1}&=2^{2(n-1)-1}-2^{(n-1)-1}+3^{n-1}\\ &=2^{2n-3}-2^{n-2}+3^{n-1}\end{aligned}

则:

gn=3gn−1+22n−3+2n−2=(4−1)(22n−3−2n−2)+3×3n−1+22n−3+2n−2=22n−1−22n−3−2n+2n−2+3n+22n−3+2n−2=22n−1−2n−1+3n\begin{aligned}g_n&=3g_{n-1}+2^{2n-3}+2^{n-2}\\ &=(4-1)(2^{2n-3}-2^{n-2})+3\times3^{n-1}+2^{2n-3}+2^{n-2}\\ &=2^{2n-1}-2^{2n-3}-2^n+2^{n-2}+3^n+2^{2n-3}+2^{n-2}\\ &=2^{2n-1}-2^{n-1}+3^n \end{aligned}

命题得证。

于是可以用快速幂做到 O(log⁡n)O(\log n),然后就愉快地切了这题……吗?

我们的做法有两个 subtask TLE 了,考虑优化。瓶颈在于快速幂的三倍常数。我们知道,在底数固定的情况下,我们可以用光速幂把这一步优化到预处理 O(p)O(\sqrt{p}),单次查询 O(1)O(1),只需设个全局变量记录一下是否初始化过即可,这样就可以通过本题了,也许可以把 11 的情况也优化到 O(1)O(1)?

代码
C++
#include<iostream>
#include<cmath>
using namespace std;
bool t;
long long s,i,h2[100000],l2[100000],h3[100000],l3[100000];
long long gsm(long long a,long long b){
	if(!t){
		t=1;
		s=sqrt(998244353)+1;
		h2[0]=l2[0]=h3[0]=l3[0]=1;
		for(i=1;i<=s;i++){
			l2[i]=l2[i-1]*2%998244353;
			l3[i]=l3[i-1]*3%998244353;
		}
		for(i=1;i<=s;i++){
			h2[i]=h2[i-1]*l2[s]%998244353;
			h3[i]=h3[i-1]*l3[s]%998244353;
		}
	}
	b%=998244352;
	if(a==2)return h2[b/s]*l2[b%s]%998244353;
	return h3[b/s]*l3[b%s]%998244353;
}
int solve(long long n,long long x,long long y){
	if(n==1||x>=n/2&&y<n/2)return 0;
	if(x<n/2&&y<n/2)return solve(n/2,x,y);
	if(x>=n/2&&y>=n/2)return solve(n/2,x-n/2,y-n/2);
	if(x>y-n/2)return (1+n/2-1)*(n/2-1)%998244353*499122177%998244353;
	return solve(n/2,x,y-n/2);
}
extern "C" int work(long long n,long long x,long long y){
	if(y>=x&&(y^x)>y-x)return solve(2ll<<__lg(max(x,y)),x,y);
	return (gsm(2,2*n-1)-gsm(2,n-1)+gsm(3,n)+998244353)%998244353;
}
原文