据说这道题根号分治是 [NOI2015] 寿司晚宴 的套路,但我能力不够,只打了 T3 的 分暴力就走人了。
讲的是或卷积的做法,并不需要用到容斥。
首先是一个暴力状压,设 表示考虑了前 个数,能整除的质数集合为 的方案数,转移很简单,但复杂度会达到 。
本题根号分治的思想是任意 最多会有一个 以上的质因数,当然需要注意 ,而 以下的素数只有 个,这样就可以状压了。
我们需要预处理出 ,注意这里的 与之前的不一样,表示拥有 及以上的质数 ,没有则 ,以及小于 的质数集合 的方案数,这样直接 dp 可以做到 ,达不到上界。
考虑如果 及以上的质数都不做要求,那样答案就是将所有 做或卷积,但如果强制 及以上的某一个质数 必须要选,考虑这样对 的影响,只是使得 减少了 ,表现在 中每一个下标都减少了 ,处理时乘上逆元即可,时间复杂度是预处理的 加上查询时的 ,代码里有注释:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+6,M=998244353;
int rv[2005],rt,zx[333];
bitset<2005>vs;
namespace fast_io{
char buf[N+5],*p1,*p2,c,f;
#define gc (p1==p2&&(p2=(p1=buf)+fread(buf,1,N,stdin),p1==p2))?EOF:*p1++
template<typename _Tp>
void read(_Tp &x){
for(c=gc,f=0;c<48;c=gc)if(c=='-')f=!f;
for(x=0;c>47;x=x*10+(48^c),c=gc);if(f)x=-x;
}
template<typename _Tp,typename..._tps>
void read(_Tp &x,_tps&...y){
read(x),read(y...);
}
char ob[N+35];
int ot,st[35],_tp;
void fls(){fwrite(ob,1,ot,stdout),ot=0;}
template<typename _Tp>
void write(_Tp x,char c){
if(x<0)ob[ot++]='-',x=-x;
for(_tp=0;x>9;st[++_tp]=48^(x%10),x/=10);
for(ob[ot++]=48^x;_tp;ob[ot++]=st[_tp--]);
ob[ot++]=c;
if(ot>N)fwrite(ob,1,ot,stdout),ot=0;
}
}using fast_io::read;
using fast_io::write;
int qp(int a,int x=M-2){快速幂求逆元
int res=1;for(;x;x>>=1,a=ll(a)*a%M)
if(x&1)res=ll(res)*a%M;return res;
}
void fwt(int *f){快速沃尔什变换求或卷积
for(int p=0;p<13;++p)
for(int k=0;k<8192;++k)
(((k>>p)&1)&&(f[k]+=f[k^(1<<p)])>=M)?f[k]-=M:0;
}
void Ifwt(int *f){
for(int p=0;p<13;++p)
for(int k=0;k<8192;++k)
(((k>>p)&1)&&(f[k]-=f[k^(1<<p)])<0)?f[k]+=M:0;
}
int n,ct[2005],q,m,a[18009],pw[N];
int f[333][8192],bs,g[N],ans,h[N];
int main(){
read(n);int i,x,y,p,fl;
for(x=pw[0]=1;x<=n;++x)
pw[x]=(pw[x-1]<<1)%M;
for(x=2;x<=2000;++x)
if(!vs[x]){
rv[zx[rt]=x]=rt,++rt;
for(y=2000/x;y;--y)vs[x*y]=1;埃氏筛素数
}
for(i=1;i<=n;++i){
read(x);
if(x==43*43)x=43需要考虑43^2 的情况;
++ct[x];
}
for(p=1;p<=rt-12;++p)f[p][0]=1初始是全部不选有一种方案;
for(y=1;y<=2000;++y)
if(ct[y]){
for(x=y,bs=0,i=0;i<13;++i)
while(!(x%zx[i]))x/=zx[i],bs|=1<<i;
x=(x==1)?1:(rv[x]-11其实不影响,让下标贴得近一些);
for(p=8191;~p;--p)
f[x][p|bs]=(ll(pw[ct[y]]-1)*f[x][p]+f[x][p|bs])%M;
暴力加入到dp数组,注意有至少选一个的2^ct-1 种方案以及不选(不变)的 1 种方案
}
for(p=1;p<=rt-12;++p)fwt(f[p]);预处理
memcpy(h,f[1],sizeof(h));
for(i=2;i<=rt-12;++i)
for(p=0;p<8192;++p)
h[p]=ll(h[p])*f[i][p]%M,f[i][p]=ll(f[i][p]+M-1)*qp(f[i][p])%M;求出不作要求的数组,然后计算撤销时需要乘的数 (x-1)*x^-1
read(q);
while(q--){
read(m),bs=0;
for(i=1;i<=m;++i)read(a[i]);
sort(a+1,a+m+1),m=unique(a+1,a+m+1)-a-1;
reverse(a+1,a+m+1);
while(m&&a[m]<=41)bs|=1<<rv[a[m--]];考虑小质数
memcpy(g,h,sizeof(g));复制
for(i=1;i<=m;++i){
x=rv[a[i]]-11;
for(p=0;p<8192;++p)
g[p]=ll(g[p])*f[x][p]%M;撤销贡献
}Ifwt(g),ans=0;逆变换,清空答案
for(p=0;p<8192;++p)
if((p&bs)==bs&&g[p])ans=(ans+g[p])%M;好像是按题意模拟?
printf("%d\n",ans);
}return 0;
}