CCF 202109-4(状态压缩 + 期望DP)

配套地址

在这里插入图片描述
在这里插入图片描述

将每一个卡牌是否选取用状压 s s s 表示。

d p ( s , j ) dp(s,j) dp(s,j) 表示 到达 状态 ( 111....11 ) (111....11) (111....11) 且 当前硬币数为 j 个还需要选的次数。

那么

i f ( s > > z ) & 1 = = 1 : if (s >> z) \& 1 == 1: if(s>>z)&1==1
d p ( s , j ) = d p ( s , j + 1 ) × p ( z ) dp(s,j) = dp(s,j+1) \times p(z) dp(s,j)=dp(s,j+1)×p(z)
e l s e : else: else
d p ( s , j ) = d p ( s ∣ ( 1 < < z ) , j ) × p ( z ) dp(s,j) = dp(s|(1<<z),j) \times p(z) dp(s,j)=dp(s(1<<z),j)×p(z)

最后
d p ( s , j ) + = 1 dp(s,j) += 1 dp(s,j)+=1

注意,当 i f ( j > = ( c n t ∗ k ) )    d p [ i ] [ j ] = 0 if(j >= (cnt * k))\ \ dp[i][j] = 0 if(j>=(cntk))  dp[i][j]=0
表示当前状态还需要0步即可到达最终状态 ( 1111....11111 ) (1111....11111) (1111....11111)

答案为 d p ( 0 , 0 ) dp(0,0) dp(0,0)

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include<bits/stdc++.h>
using namespace std;
const int N = 16;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
//#define int long long
typedef long long ll;
typedef pair<int,int> pii;

double dp[1<<N][N*N];
double p[N];

signed main(){
	IOS
	#ifdef ddgo
		freopen("C:\\Users\\asus\\Desktop\\ddgoin.txt","r",stdin);
    #endif
    int n,k; cin>>n>>k;
    for(int i=0;i<n;i++) cin>>p[i];
    dp[(1<<n)-1][0] = 0;
    for(int i=(1<<n)-2;i>=0;i--){
    	for(int j=k*n;j>=0;j--) {
	    	for(int z=0;z<n;z++) {
	    		if((i >> z) & 1) dp[i][j] += dp[i][j+1] * p[z];
	    		else dp[i][j] += dp[i | (1 << z)][j] * p[z];
			}
			dp[i][j] += 1;
			int cnt = n;
			for(int z=0;z<n;z++) if((i >> z) & 1) cnt --;
			if(j >= (cnt * k)) dp[i][j] = 0;
		}
	}
    cout<<fixed<<setprecision(10)<<dp[0][0]<<endl;
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值