题解 P7738 [NOI2021] 量子通信

· · 题解

写一个自认为详细的题解?

首先考虑暴力,对于每一次询问暴力枚举匹配串,令 L=256,表示串长,时间复杂度 O(nmL) 或 O(\dfrac{nmL}\omega )。

发现这个暴力完全没有扩展空间,考虑发掘题目性质。

$2.$ $k_i\leq 15$。 $3.$ 强制在线是假的。 $4.$ 我们只需要判断是否存在,不需要计数。 $5.$ 原串是随机的,但询问不是。 这些条件使得题目变得可做,但并不需要全部用上。 看到 $k_i\leq 15$,但不是状压,也没法枚举,可以与定值 $256$ 产生联系。 发现若将一个长度为 $256$ 的串分为 $16$ 段,根据鸽巢原理,那么修改 $k_i$ 个数之后至少有一段没变! $2^{16}=65536$,那么其中的某一段值相同的期望只有 $\dfrac n {65536}$ 个,那么对于每一次询问,可以直接暴力查询每一段相同的几个串,用 bitset 优化匹配即可。 ~~于是我们并没有用到性质 3,4~~ 实际上,由于询问不是随机的,上面的期望可以卡出一个二倍的常数,所以需要进行一些常数优化(但感觉数据没卡): $1.$ 不要访问 bitset,非常慢。 $2.$ 可以用四个 unsigned long long 直接代替一个 bitset。 $3.$ 用链表不要用 vector。 由于考场卡常,且不确定能不能用 popcount,很多东西是手写的,稍稍有点长: ```cpp #include<bits/stdc++.h> using namespace std; #define re register #define ull unsigned long long int st; char s[400002][256],Q[256]; char rbuf[32000002]; int pt=-1; ull myRand(ull &k1, ull &k2) { ull k3=k1,k4=k2; k1=k4; k3^=(k3<<23); k2=k3^k4^(k3>>17)^(k4>>26); return k2+k4; } void gen(int n, ull a1, ull a2) { for(int i=1;i<=n;++i) for(int j=0;j<256;++j) s[i][j]=(myRand(a1, a2) & (1ull << 32)) ? 1 : 0; } int n,m,cnt,la,head[1048576],popcnt[65536]; struct edge{ int to,next; }e[6400002]; inline void add(re int x,re int y){e[++cnt]=(edge){y,head[x]},head[x]=cnt;} ull a1,a2; inline void read(char* A){ re char v=rbuf[++pt]; while((v<'0'||v>'9')&&(v<'A'||v>'F'))v=rbuf[++pt]; re int tot=0; while((v>='0'&&v<='9')||(v>='A'&&v<='F')){ re int tmp=(v>='A')?v-'A'+10:v-'0'; A[tot++]=(tmp&8)>0,A[tot++]=(tmp&4)>0,A[tot++]=(tmp&2)>0,A[tot++]=tmp&1; v=rbuf[++pt]; } } ull A[400002],B[400002],C[400002],D[400002]; inline ull read(){ re ull t=0;re char v=rbuf[++pt]; while(v<'0')v=rbuf[++pt]; while(v>='0')t=(t<<3)+(t<<1)+v-48,v=rbuf[++pt]; return t; } inline int P(re ull x){return popcnt[x&65535]+popcnt[(x>>16)&65535]+popcnt[(x>>32)&65535]+popcnt[(x>>48)&65535];} int ed; int main(){ // freopen("qi.in","r",stdin); // freopen("qi.out","w",stdout); fread(rbuf,1,32000000,stdin),n=read(),m=read(),a1=read(),a2=read(),gen(n,a1,a2); for(re int i=1;i<=65536;++i)popcnt[i]=popcnt[i>>1]+(i&1); for(re int i=1;i<=n;++i){ for(re int j=0;j<64;++j)A[i]=(A[i]<<1)+s[i][j]; for(re int j=64;j<128;++j)B[i]=(B[i]<<1)+s[i][j]; for(re int j=128;j<192;++j)C[i]=(C[i]<<1)+s[i][j]; for(re int j=192;j<256;++j)D[i]=(D[i]<<1)+s[i][j]; for(re int j=0;j<4;++j)add((j<<16)+((A[i]>>(((j&3)^3)<<4))&65535),i); for(re int j=4;j<8;++j)add((j<<16)+((B[i]>>(((j&3)^3)<<4))&65535),i); for(re int j=8;j<12;++j)add((j<<16)+((C[i]>>(((j&3)^3)<<4))&65535),i); for(re int j=12;j<16;++j)add((j<<16)+((D[i]>>(((j&3)^3)<<4))&65535),i); } while(m--){ read(Q);re int K=read(),ss=0,vv=0; re ull a=0,b=0,c=0,d=0; for(re int j=0;j<256;++j)Q[j]=Q[j]^la; for(re int j=0;j<64;++j)a=(a<<1)+Q[j]; for(re int j=64;j<128;++j)b=(b<<1)+Q[j]; for(re int j=128;j<192;++j)c=(c<<1)+Q[j]; for(re int j=192;j<256;++j)d=(d<<1)+Q[j]; for(re int j=0;j<4&&ss==0;++j){ re int T=j*65536+((a>>(((j&3)^3)<<4))&65535); for(re int k=head[T];k;k=e[k].next){ re int x=e[k].to; if(P(a^A[x])+P(b^B[x])+P(c^C[x])+P(d^D[x])<=K){ss=1;break;} } } for(re int j=4;j<8&&ss==0;++j){ re int T=j*65536+((b>>(((j&3)^3)<<4))&65535); for(re int k=head[T];k;k=e[k].next){ re int x=e[k].to; if(P(a^A[x])+P(b^B[x])+P(c^C[x])+P(d^D[x])<=K){ss=1;break;} } } for(re int j=8;j<12&&ss==0;++j){ re int T=j*65536+((c>>(((j&3)^3)<<4))&65535); for(re int k=head[T];k;k=e[k].next){ re int x=e[k].to; if(P(a^A[x])+P(b^B[x])+P(c^C[x])+P(d^D[x])<=K){ss=1;break;} } } for(re int j=12;j<16&&ss==0;++j){ re int T=j*65536+((d>>(((j&3)^3)<<4))&65535); for(re int k=head[T];k;k=e[k].next){ re int x=e[k].to; if(P(a^A[x])+P(b^B[x])+P(c^C[x])+P(d^D[x])<=K){ss=1;break;} } } printf("%d\n",la=ss); } return 0; } ```