题解:P16029 [CSPro 23] 收集卡牌
yuyue_is_not_human · · 题解
:::info[题意]{open}
给定
问你集齐所有卡的期望抽卡次数。
:::
O(2^nn^2k)
记
此时
转移时,枚举所有卡牌
时间复杂度
:::info[Code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
const int N=17,K=5;
int n,k;double p[N],f[1<<N][N*K],ans;
signed main(){
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>p[i],f[1<<i-1][0]=p[i];
for(int S=1;S<(1<<n)-1;S++){
int t=__builtin_popcount(S);
for(int i=0;i<=n*k;i++) if(f[S][i]){
if(i>=k*(n-t)) continue;
for(int j=1;j<=n;j++){
if((S>>(j-1))&1){
if(i+1<=n*k) f[S][i+1]+=p[j]*f[S][i];
}else f[S|(1<<(j-1))][i]+=p[j]*f[S][i];
}
}
}
for(int S=1;S<(1<<n);S++)
for(int i=0;i<=n*k;i++) if(f[S][i]&&__builtin_popcount(S)+i/k>=n)
ans+=f[S][i]*(__builtin_popcount(S)+i);
printf("%.7Lf",ans);
}
:::
O(2^nnk)
记总抽卡次数为
那么抽卡停止
我们知道
对
于是
换序对定
记住这个式子,我们很快会再见的。
注意到最内层是一个区间求和,预处理
:::info[Code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define ctz __builtin_ctz
const int N=17,M=1<<N,D=200;
int n,k,pc[M];
double p[20],sp[M],C[20][20],pf[D];
signed main(){
ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>p[i];
for(int S=1;S<(1<<n);S++) sp[S]=sp[S^(S&-S)]+p[ctz(S&-S)+1],pc[S]=pc[S^(S&-S)]+1;
for(int i=0;i<=n;i++) for(int j=C[i][0]=C[i][i]=1;j<i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];
double ans=1,P,w,G,e;int t,b,R,L,d=k*n;
for(int S=1;S<(1<<n)-1;S++){
t=pc[S],P=sp[S],w=1,pf[0]=1;
for(int m=1;m<=d;m++) w*=P,pf[m]=pf[m-1]+w;
for(b=t;b<n;b++) R=k*n-(k-1)*b-1,L=b,G=pf[R]-pf[L-1],e=C[n-t][b-t]*G,ans+=((b-t)&1)?-e:e;
}printf("%.10Lf",ans);
}
:::
O(2^n (k+\log n))
回到我们的式子。注意最内层本质是等比求和。
固定
所以
其中
二项式定理处理一下
:::info[Code]
#include<bits/stdc++.h>
using namespace std;
#define double long double
#define ctz __builtin_ctz
const int N=17,M=1<<N;
int n,k,pc[M];double p[20],sp[M],C[20][20];
signed main(){
ios::sync_with_stdio(false);cin.tie(NULL);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>p[i];
for(int S=1;S<(1<<n);S++) sp[S]=sp[S^(S&-S)]+p[ctz(S&-S)+1],pc[S]=pc[S^(S&-S)]+1;
for(int i=0;i<=n;i++) for(int j=C[i][0]=C[i][i]=1;j<i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];
double ans=1;
for(int S=1;S<(1<<n)-1;S++){
int t=pc[S],m0=n-t;
double P=sp[S];
if(fabsl(1-P)<1e-18) continue;
double q=powl(P,-(k-1));
double A=powl(P,t)*(powl(1-P,m0)-((m0&1)?-powl(P,m0):powl(P,m0)));
double B=powl(P,k*n-(k-1)*t)*(powl(1-q,m0)-((m0&1)?-powl(q,m0):powl(q,m0)));
ans+=(A-B)/(1-P);
}printf("%.10Lf",ans);
}
:::
O(n^4k^2) \to O(n^3k^2) \to O(n^3k\log k)
再次回到这个式子。
考虑用另一种方式刻画
其中
它是一个次数不超过
使用一些小暴力卷积可以做到
O(n^2k \log(nk) \log n)
本文的公式部分使用了 AI 进行润色。