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

· · 题解

好题。

设 a\&b 表示 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>b 其实是一定成立的,因为 a>b\ge a\&b。

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

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

\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

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

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

\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}

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

::::success[对递归结构的证明] 考虑数学归纳法。

当 n<2 显然成立。

当 n\ge2,分类讨论:

命题得证。

再考虑 0。判断一个位置是否是 0 是简单的,直接求一下 f_{n,x,y} 是否为 0 即可。(赛时犯唐了,别管这句话。)显然所有的 0 在一个连通块内,所以只需关注全局 0 的总数。设 g_{n} 表示 2^n\times2^n 矩阵中 0 的个数,根据递归结构易知 g_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 列出前几项(下标从 0 开始):

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

一眼瞪出 g_n=2^{2n-1}-2^{n-1}+3^n。

::::success[对通项公式的证明] 考虑数学归纳法。

当 n=0 显然成立。

当 n>0,若:

&=2^{2n-3}-2^{n-2}+3^{n-1}\end{aligned}

则:

&=(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),然后就愉快地切了这题……吗?

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

::::success[代码]

#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;
}

::::