HDU-6801 2020HDU多校第三场T11 (生成函数)
题解又给式子不解释了。。
设未被选中的概率(q=1-p)
设(a_i)为(c)号点被选中前有(i)个点被选中的概率,它的普通生成函数为(A(x))
考虑枚举(c)在第(i)次被访问到时被选中
则(c)前面的(c-1)个点在转的过程中被访问了(i)次,后面的(n-c)个点被访问了(i-1)次(可能在没有经过这么多次时就已经被删掉了,但是不影响概率的计算),因此可以简单考虑这两种点在(c)之前被选中的概率
得到一个(A(x))的表示
(egin{aligned} A(x)=sum_{i=1}^{infty}pcdot q^{i-1}cdot (q^i+(1-q^i)x)^{c-1}cdot (q^{i-1}+(1-q^{i-1}x))^{n-c}end{aligned})
也即题解中的式子
(egin{aligned} A(x)=sum_{i=0}^{infty}pcdot q^icdot (q^{i+1}+(1-q^{i+1})x)^{c-1}cdot (q^i+(1-q^ix))^{n-c}end{aligned})
然后是暴力展开
(egin{aligned} A(x)=sum_{i=0}^{infty}pcdot q^icdot (q^{i+1}(1-x)+x)^{c-1}cdot (q^i(1-x)+x)^{n-c}end{aligned})
(egin{aligned} A(x)=sum_{i=0}^{infty}pcdot q^icdot (sum_{j=0}^{c-1}C(c-1,j)cdot q^{(i+1)j}(1-x)^jx^{c-1-j})cdot (sum_{k=0}^{n-c}C(n-c,k)cdot q^{ik}(1-x)^kx^{n-c-k})end{aligned})
(egin{aligned} A(x)=sum_{i=0}^{infty}pcdot q^icdot sum_{j=0}^{c-1}sum_{k=0}^{n-c} C(c-1,j)cdot C(n-c,k)cdot q^{(i+1)j+ik}(1-x)^{j+k}x^{n-j-k-1}end{aligned})
这个式子极其反人类,把(i)换到右边化掉
(egin{aligned} A(x)=sum_{j=0}^{c-1}sum_{k=0}^{n-c} C(c-1,j)cdot C(n-c,k)cdot (1-x)^{j+k}x^{n-j-k-1}cdot sum_{i=0}^{infty}pcdot q^i q^{(i+1)j+ik}end{aligned})
右边是一个收敛的无穷等比数列
(egin{aligned} A(x)=sum_{j=0}^{c-1}sum_{k=0}^{n-c} C(c-1,j)cdot C(n-c,k)cdot (1-x)^{j+k}x^{n-j-k-1}cdot frac{pq^j}{1-q^{j+k+1}}end{aligned})
(egin{aligned} A(x)=sum_{j=0}^{c-1}sum_{k=0}^{n-c} C(c-1,j)cdot C(n-c,k)cdot x^{n-j-k-1}frac{pq^j}{1-q^{j+k+1}}cdot (sum_{i=0}^{j+k} C(j+k,i) cdot (-x)^i)end{aligned})
虽然有三个循环,但是很显然可以先对于(j,k)进行一次卷积,然后再对于(j+k,-i)进行一次卷积得到
Code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef pair <int,int> Pii;
#define mp make_pair
#define pb push_back
#define Mod1(x) ((x>=P)&&(x-=P))
#define Mod2(x) ((x<0)&&(x+=P))
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); }
template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
char IO;
template <class T=int> T rd(){
T s=0; int f=0;
while(!isdigit(IO=getchar())) if(IO=='-') f=1;
do s=(s<<1)+(s<<3)+(IO^'0');
while(isdigit(IO=getchar()));
return f?-s:s;
}
const int N=1<<21,P=998244353;
int n,c,p,q;
ll qpow(ll x,ll k=P-2) {
ll res=1;
for(;k;k>>=1,x=x*x%P) if(k&1) res=res*x%P;
return res;
}
int w[N],Fac[N+1],Inv[N+1],FInv[N+1],rev[N];
void Init(){
Fac[0]=Fac[1]=Inv[0]=Inv[1]=FInv[0]=FInv[1]=1;
rep(i,2,N) {
Fac[i]=1ll*Fac[i-1]*i%P;
Inv[i]=1ll*(P-P/i)*Inv[P%i]%P;
FInv[i]=1ll*FInv[i-1]*Inv[i]%P;
}
w[N>>1]=1; ll t=qpow(3,(P-1)/N);
rep(i,(N>>1)+1,N-1) w[i]=w[i-1]*t%P;
drep(i,(N>>1)-1,1) w[i]=w[i<<1];
}
int Init(int n){
int R=1,c=-1;
while(R<n) R<<=1,c++;
rep(i,1,R-1) rev[i]=(rev[i>>1]>>1)|((i&1)<<c);
return R;
}
void NTT(int n,int *a,int f) {
rep(i,1,n-1) if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int i=1;i<n;i<<=1) {
int *e=w+i;
for(int l=0;l<n;l+=i*2) {
for(int j=l;j<l+i;++j) {
int t=1ll*a[j+i]*e[j-l]%P;
a[j+i]=a[j]-t; Mod2(a[j+i]);
a[j]+=t; Mod1(a[j]);
}
}
}
if(f==-1) {
reverse(a+1,a+n);
rep(i,0,n-1) a[i]=1ll*a[i]*Inv[n]%P;
}
}
int A[N],B[N],C[N];
int main(){
Init();
rep(kase,1,rd()) {
n=rd(),p=rd(),q=rd(),p=p*qpow(q)%P,q=P+1-p,c=rd();
int R=Init(n),t=1;
rep(i,0,R-1) A[i]=B[i]=C[i]=0;
rep(i,0,c-1) {
A[i]=1ll*Fac[c-1]*FInv[i]%P*FInv[c-1-i]%P*t%P;
t=1ll*t*q%P; // t= q^i
}
rep(i,0,n-c) B[i]=1ll*Fac[n-c]*FInv[i]%P*FInv[n-c-i]%P;
NTT(R,A,1),NTT(R,B,1);
rep(i,0,R-1) A[i]=1ll*A[i]*B[i]%P;
NTT(R,A,-1);
R=Init(n*2);
rep(i,n,R-1) A[i]=0;
rep(i,0,R-1) B[i]=0;
t=1;
rep(i,0,n-1) {
t=1ll*t*q%P; // t=q^{i+1}
A[i]=1ll*A[i]*Fac[i]%P*qpow(P+1-t)%P;
}
rep(i,0,n) B[n-i]=(i&1)?P-FInv[i]:FInv[i];
NTT(R,A,1),NTT(R,B,1);
rep(i,0,R-1) A[i]=1ll*A[i]*B[i]%P;
NTT(R,A,-1);
rep(i,1,n) printf("%d
",int(1ll*FInv[n-i]*A[2*n-i]%P*p%P));
}
}
实际跑两次卷积,写得丑几乎就是顶着时限过去的。。