BZOJ 3992 序列统计

Link

Solution

从一个集合中取元素,每个元素可以取无限次,问乘积模mmxx的序列有多少个 看一眼排列似乎是指数生成函数,然而那样就似乎会相当麻烦 注意到每种元素可以取无限次,于是序列的每个位置的元素选取都是独立不受影响的,就可以看成从nn个集合各取一个元素的方案数了,然后就可以套普通生成函数了 下一步需要把答案构造成函数某个项的系数,也就是说项的次数与xx有关,系数为方案数。但是xx的转移是乘积取模的形式,这个不好。为了去掉乘,就把方程取个log\log,哦不对是取个 。然后就好了。 在IDFT之后,在计算结果的时候把ii模一个ϕ(m)=m1\phi(m)=m-1。快速幂做完就行了。

Tips

各路大神似乎都把 当成了00,但是根据定义 ,强迫症患者表示不爽。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//Code by Lucida
#include<bits/stdc++.h>
#define get(x) scanf("%d",&x)
#define put(x) printf("%d",x)
typedef long long ll;
template <class T> inline bool chkmx(T &a,const T &b){return a<b?a=b,1:0;}
template <class T> inline bool chkmn(T &a,const T &b){return a>b?a=b,1:0;}
const int P=1004535809,LP=21,G=3,MAXM=8000+11,LEN=MAXM<<2;//LEN=MAXM<<1... 2M需要两倍空间
using std::swap;
using std::reverse;

template <class T> T Pow(T base,int index)
{
T res;res.I();//res默认设置单位I,在其他地方会出问题
for(;index;base*=base,index>>=1)
if(index&1) res*=base;
return res;
}
namespace _NTT_
{
const int modu=P;
struct ModInt
{
ll num;
ModInt(ll x=0):num(x%modu){}
void I(){num=1;}
ModInt operator +(ModInt b){return ModInt(num+b.num);}
ModInt operator -(ModInt b){return ModInt((num-b.num)%modu+modu);}
ModInt operator *(ModInt b){return ModInt(num*b.num);}
ModInt operator *(ll b){return ModInt(num*b);}
ModInt &operator =(ll x){return num=x%modu,*this;}
template <class T> ModInt &operator +=(T b){return *this=*this+b;}
template <class T> ModInt &operator -=(T b){return *this=*this-b;}
template <class T> ModInt &operator *=(T b){return *this=*this*b;}
};
struct Wing
{
ModInt wn[LP+1];
Wing()
{
ModInt g=G;
for(int i=0;i<=LP;++i)
wn[i]=Pow(g,(P-1)/(1<<i));
}
ModInt &operator [](int x){return wn[x];}
}wn;
void Trans(ModInt a[],int n)
{
for(int i=1,j=n>>1;i<n-1;++i)
{
if(i<j) swap(a[i],a[j]);
int k=n>>1;
while(j>=k)
{
j-=k;
k>>=1;
}
j+=k;
}
for(int d=2,index=1;d<=n;d<<=1,index++)//d<n..
{
for(int i=0;i<n;i+=d)
{
ModInt w=1;
for(int j=i;j<i+(d>>1);++j)
{
ModInt u=a[j],t=w*a[j+(d>>1)];
a[j]=u+t,a[j+(d>>1)]=u-t;
w*=wn[index];
}
}
}
}
void DFT(ModInt a[],int n)
{
Trans(a,n);
}
void IDFT(ModInt a[],int n)
{
Trans(a,n);
reverse(a+1,a+n);
ModInt inv=Pow(ModInt(n),P-2);
for(int i=0;i<n;++i)
a[i]*=inv;
}
}
int m;
struct Polynom
{
int a[LEN],n;
Polynom()
{
memset(a,0,sizeof(a));
n=0;
}
void I(){a[0]=n=1;}
int &operator [](int x){return a[x];}
Polynom &operator *=(Polynom&);
};
Polynom operator *(Polynom &a,Polynom &b)
{
using namespace _NTT_;
static ModInt A[LEN],B[LEN];static Polynom c;
int n;
for(n=1,c.n=a.n+b.n-1;n<c.n;n<<=1);//n>>=1..
for(int i=0;i<n;++i)
{
A[i]=i<a.n?a[i]:0;
B[i]=i<b.n?b[i]:0;//a[i]!!!
c[i]=0;
}
DFT(A,n),DFT(B,n);
for(int i=0;i<n;++i)
A[i]*=B[i];
IDFT(A,n);
for(int i=0;i<n;++i)
(c[i%(m-1)]+=A[i].num)%=P;
c.n=m+1;
return c;
}
Polynom &Polynom::operator *=(Polynom &a){return *this=*this*a;}

int Div(int x,int p[])
{
int pc=0,m=(int)sqrt(x+0.5)+1;
for(int i=2;i<=m;++i)
{
if(x%i==0)
{
do x/=i;
while(x%i==0);
p[++pc]=i;
}
}
if(!x) p[++pc]=x;
return pc;
}
int sub[MAXM];
int Pow(ll base,int index,int P)
{
ll res;
for(res=1;index;index>>=1,(base*=base)%=P)
if(index&1) (res*=base)%=P;
return res;
}
int prim_root(int p)
{
int ph=p-1,dc=Div(ph,sub);
for(int g=2;g<P;++g)
{
bool check=1;
for(int i=1;i<=dc && check;++i)
if(Pow(g,ph/sub[i],p)==1)
check=0;
if(check)
return g;
}
return -1;
}
int ind[MAXM];
void AllInd(int p)
{
ll w=1,g=prim_root(p);
for(int i=0;i<=p-1;++i)
{
ind[w]=i;
(w*=g)%=p;
}
}
int main()
{
static int s[MAXM];
static Polynom a,f;
int n,x,sc;
get(n),get(m),get(x),get(sc);
AllInd(m);
for(int i=1;i<=sc;++i)
{
get(s[i]);
if(!s[i]) continue;
a[ind[s[i]]]++;
}
a.n=m+1;
f=Pow(a,n);
int Ans=f[ind[x]];
put(Ans),putchar('\n');
return 0;
}
/* AC Record(Bugs)

*/