状压DP:

先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 1008, INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
double dp[1 << 21];
double ans[30];
double p[30];
int main(){
int n, k;
cin>>n>>k;
int cnt = 0;
for(int i= 0; i < n; i++){
scanf("%lf", &p[i]);
if(p[i] < 1e-6){
cnt++;
}
}
k = min(k, n - cnt);
if(n <= k){
printf("1.0");
for(int i = 1; i < n; i++){
printf(" 1.0");
}
printf("\n");
}else{
dp[0] = 1;
for(int i = 0; i < (1 << n); i++){
double sum = 0;
for(int j = 0; j < n; j++){
if((i & (1 << j) ) == 0){
sum += p[j];
}
}
//cout<<sum<<"#\n";
for(int j = 0 ; j < n; j++){
if( (i & (1 << j) ) == 0){
dp[i | (1 << j)] += dp[i] * p[j] / sum;
}
}
}
for(int i = 0; i < (1 << n); i++){
int cnt = 0;
int tp = i;
while(tp){
cnt++;
tp = tp & (tp - 1);
}
if(cnt == k){
for(int j = 0; j < n; j++){
if(i & (1 << j)){
ans[j] += dp[i];
}
}
} } printf("%.8f", ans[0]);
for(int i = 1; i < n; i++){
printf(" %.8f", ans[i]);
}
printf("\n"); } return 0;
}

  

Codeforces Round #363 LRU(概率 状压DP)的更多相关文章

  1. NOIP2016提高A组 A题 礼物—概率状压dp

    题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生 日礼物. 商店里一共有n种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种礼物的喜悦值不能重复获得). 每次,店员会 ...

  2. codeforces 8C. Looking for Order 状压dp

    题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...

  3. Codeforces 429C Guess the Tree(状压DP+贪心)

    吐槽:这道题真心坑...做了一整天,我太蒻了... 题意 构造一棵 $ n $ 个节点的树,要求满足以下条件: 每个非叶子节点至少包含2个儿子: 以节点 $ i $ 为根的子树中必须包含 $ c_i ...

  4. Codeforces 895C Square Subsets(状压DP 或 异或线性基)

    题目链接  Square Subsets 这是白书原题啊 先考虑状压DP的做法 $2$到$70$总共$19$个质数,所以考虑状态压缩. 因为数据范围是$70$,那么我们统计出$2$到$70$的每个数的 ...

  5. 【题解】codeforces 8c Looking for Order 状压dp

    题目描述 Lena喜欢秩序井然的生活.一天,她要去上大学了.突然,她发现整个房间乱糟糟的--她的手提包里的物品都散落在了地上.她想把所有的物品都放回她的手提包.但是,这里有一点问题:她一次最多只能拿两 ...

  6. Codeforces 895C Square Subsets:状压dp【组合数结论】

    题目链接:http://codeforces.com/problemset/problem/895/C 题意: 给你n个数a[i].(n <= 10^5, 1 <= a[i] <= ...

  7. Codeforces Round #385 //再遇状压

    敲完三题挂机一小时.....  也没懂DE什么意思  rank600上了一波分... A. Hongcow Learns the Cyclic Shift 给一个字符串,每次可以把最后一个字符拿到开头 ...

  8. CodeForces 599E Sandy and Nuts 状压DP

    题意: 有一棵\(n(1 \leq n \leq 13)\)个节点的树,节点的标号为\(1 \sim n\),它的根节点是\(1\). 现在已知它的\(m(0 \leq m < n)\)条边,和 ...

  9. [BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)

    https://loj.ac/problem/2290 题解:https://blog.csdn.net/Vectorxj/article/details/78905660 不是很好理解,对于边(x1 ...

随机推荐

  1. Python自动化之多进程

    多进程multiprocessing from multiprocessing import Process import os def info(title): print(title) print ...

  2. PE355

    似乎我和lyx讨论过这题..? LP可解决..(~0.8s)

  3. Objective C 内存管理[转]

    1  配对原则 alloc – release new – release retain - release copy – release 2  new和alloc-init的区别 (1)区别只在于a ...

  4. 解决Python往MySQL插入中文时报错的问题

    今天遇到一个问题,用Python往MySQL插入数据时,若数据中包含中文会报类似下面的错误: ERROR 1366: Incorrect string value: '\xE4\xB8\xAD\xE5 ...

  5. java的事务处理

    本文转自http://zhenchengchagangzi.iteye.com/blog/1159493 java的事务处理,如果对数据库进行多次操作,每一次的执行或步骤都是一个事务.如果数据库操作在 ...

  6. Graph Valid Tree

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  7. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  8. docker 初探

    2015-08-14 09:24:42 查看本地已经有的镜像: docker images 启动镜像实例(例centos), 并进入centos命令行提示符 (镜像的一个实例就叫container): ...

  9. nginx 原理&知识

    2015年6月4日 17:04:20 星期四 发现两个关于nginx原理的系列文章, 非常好 http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html ...

  10. selenium使用actions.moveToElement处理菜单

    //should set firefox path //FirefoxBinary binary=new FirefoxBinary(new File("C:\\Program Files ...