【LOJ #2271】「SDOI2017」遗忘的集合(多项式Ln+任意模数NTT)
简单推到可以得到
设
那么
于是对于从小到大枚举每个数枚举倍数更新即可
由于极其恶心的任意模数,要写
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define re register
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
int mod;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a+=b;(a>=mod)?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
struct plx{
double x,y;
plx(double _x=0,double _y=0):x(_x),y(_y){}
friend inline plx operator +(cs plx &a,cs plx &b){
return plx(a.x+b.x,a.y+b.y);
}
friend inline plx operator -(cs plx &a,cs plx &b){
return plx(a.x-b.x,a.y-b.y);
}
friend inline plx operator *(cs plx &a,cs plx &b){
return plx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
}
inline plx conj()cs{return plx(x,-y);}
};
#define poly vector<int>
cs int C=21,M=(1<<15)-1;
cs double pi=acos(-1);
vector<plx> w[C+1];
int rev[(1<<C)|5],inv[(1<<C)|5];
inline void init_rev(int lim){
for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
inline void init_w(){
for(int i=1;i<=C;i++)w[i].resize(1<<(i-1));
plx wn=plx(cos(pi/(1<<(C-1))),sin(pi/(1<<(C-1))));
w[C][0]=plx(1,0);
inv[0]=inv[1]=1;
for(int i=2;i<(1<<(C-1));i++)inv[i]=mul(mod-mod/i,inv[mod%i]);
for(int i=1;i<(1<<(C-1));i++){
if(i&31)w[C][i]=w[C][i-1]*wn;
else w[C][i]=plx(cos(pi*i/(1<<(C-1))),sin(pi*i/(1<<(C-1))));
}
for(int i=C-1;i;i--)
for(int j=0;j<(1<<(i-1));j++)w[i][j]=w[i+1][j<<1];
}
inline void fft(plx *f,int lim,int kd){
for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
plx a0,a1;
for(int mid=1,l=1;mid<lim;mid<<=1,l++)
for(int i=0;i<lim;i+=(mid<<1))
for(int j=0;j<mid;j++)
a0=f[i+j],a1=f[i+j+mid]*w[l][j],f[i+j]=a0+a1,f[i+j+mid]=a0-a1;
if(kd==-1){
reverse(f+1,f+lim);
for(int i=0;i<lim;i++)f[i].x/=lim,f[i].y/=lim;
}
}
inline poly operator *(poly A,poly B){
static plx a[(1<<C)|5],b[(1<<C)|5],c[(1<<C)|5],d[(1<<C)|5],da,db,dc,dd;
int deg=A.size()+B.size()-1,lim=1;
while(lim<deg)lim<<=1;
A.resize(lim),B.resize(lim);
for(int i=0;i<lim;i++)a[i]=plx(A[i]&M,A[i]>>15),b[i]=plx(B[i]&M,B[i]>>15);
init_rev(lim);
fft(a,lim,1),fft(b,lim,1);
for(int i=0;i<lim;i++){
int j=(lim-i)&(lim-1);
da=(a[i]+a[j].conj())*plx(0.5,0);
db=(a[j].conj()-a[i])*plx(0,0.5);
dc=(b[i]+b[j].conj())*plx(0.5,0);
dd=(b[j].conj()-b[i])*plx(0,0.5);
c[i]=(da*dc)+((da*dd)*plx(0,1));
d[i]=(db*dd)+((db*dc)*plx(0,1));
}
fft(c,lim,-1),fft(d,lim,-1);
poly ret(lim);
for(int i=0;i<lim;i++){
ll da=(ll)(d[i].x+0.5)%mod,db=(ll)(d[i].y+0.5)%mod,dc=(ll)(c[i].y+0.5)%mod,dd=(ll)(c[i].x+0.5)%mod;
ret[i]=((da<<30)+((db+dc)<<15)+dd)%mod;
}
return ret;
}
inline poly Inv(poly a,int deg){
poly b(1,Inv(a[0])),c;
for(int lim=4;lim<(deg<<2);lim<<=1){
init_rev(lim);
c=a,c.resize(lim>>1);
b.resize(lim),c.resize(lim);
c=c*b*b;
for(int i=0;i<(lim>>1);i++)
b[i]=dec(mul(2,b[i]),c[i]);
b.resize(lim>>1);
}
b.resize(deg);return b;
}
inline poly deriv(poly a){
for(int i=0;i+1<a.size();i++)a[i]=mul(a[i+1],i+1);
a.pop_back();return a;
}
inline poly integ(poly a){
a.pb(0);
for(int i=(int)a.size()-1;i;i--)a[i]=mul(a[i-1],inv[i]);
a[0]=0;return a;
}
inline poly Ln(poly a,int deg){
a=integ(Inv(a,deg)*deriv(a)),a.resize(deg);return a;
}
cs int N=(1<<19)|5;
int n,ans[N];
poly f;
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=read(),mod=read();
init_w();
f.resize(n+1),f[0]=1;
for(int i=1;i<=n;i++)f[i]=read();
f=Ln(f,n+1);
for(int i=1;i<=n;i++)Mul(f[i],i);
for(int i=1;i<=n;i++){
for(int j=i+i;j<=n;j+=i)
Dec(f[j],f[i]);
}
int cnt=0;
for(int i=1;i<=n;i++)if(f[i])cnt++;
cout<<cnt<<'\n';
for(int i=1;i<=n;i++)if(f[i])cout<<i<<" ";
}