Lucky Coins

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5985

Description

Bob has collected a lot of coins in different kinds. He wants to know which kind of coins is lucky. He finds out a lucky kind of coins by the following way. He tosses all the coins simultaneously, and then removes the coins that come up tails. He then tosses all the remaining coins and removes the coins that come up tails. He repeats the previous step until there is one kind of coins remaining or there are no coins remaining. If there is one kind of coins remaining, then this kind of coins is lucky. Given the number of coins and the probability that the coins come up heads after tossing for each kind, your task is to calculate the probability for each kind of coins that will be lucky.

Input

The first line is the number of test cases. For each test case, the first line contains an integer k representing the number of kinds. Each of the following k lines describes a kind of coins, which contains an integer and a real number representing the number of coins and the probability that the coins come up heads after tossing. It is guaranteed that the number of kinds is no more than 10, the total number of coins is no more than 1000000, and the probabilities that the coins come up heads after tossing are between 0.4 and 0.6.

Output

For each test case, output a line containing k real numbers with the precision of 6 digits, which are the probabilities of each kind of coins that will be lucky.

Sample Input

3

1

1000000 0.5

2

1 0.4

1 0.6

3

2 0.4

2 0.5

2 0.6

Sample Output

1.000000

0.210526 0.473684

0.124867 0.234823 0.420066

Hint

题意

有一堆硬币,每回合为正面的概率为P,每回合我们都会去掉当前翻面为反面的硬币。

问每种硬币剩到只剩下一个的概率是多少。

保证 0.4<P<0.6

题解:

给了概率的范围,显然这道题就是模拟扔就行了,随便扔个几十回合,这个概率就会降到很小的范围。

第i个硬币第j回合全死掉的概率为 (1-Pj)num[i]

活下来的概率当然是1-死掉的。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 15;
  4. int n;
  5. double num[maxn],ans[maxn],p[maxn];
  6. double count_die(int x,int y){
  7. return pow(1-pow(p[x],y),num[x]);
  8. }
  9. double count_live(int x,int y){
  10. return 1-count_die(x,y);
  11. }
  12. void solve(){
  13. scanf("%d",&n);
  14. memset(ans,0,sizeof(ans));
  15. for(int i=0;i<n;i++)
  16. cin>>num[i]>>p[i];
  17. if(n==1){
  18. printf("1.000000\n");
  19. return;
  20. }
  21. for(int i=1;i<100;i++){
  22. for(int j=0;j<n;j++){
  23. double tmp = 1;
  24. for(int k=0;k<n;k++){
  25. if(k==j)continue;
  26. tmp*=count_die(k,i);
  27. }
  28. ans[j]+=(count_live(j,i)-count_live(j,i+1))*tmp;
  29. }
  30. }
  31. for(int i=0;i<n;i++)
  32. if(i==0)printf("%.6f",ans[i]);
  33. else printf(" %.6f",ans[i]);
  34. printf("\n");
  35. }
  36. int main(){
  37. int t;
  38. scanf("%d",&t);
  39. while(t--)solve();
  40. }

HDU 5985 Lucky Coins 数学的更多相关文章

  1. HDU 5985 Lucky Coins(概率)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5985 题意:有多种类型的硬币,每种类型的硬币都有一定的数量,现在每次抛硬币,除去朝下的硬币,知道最后 ...

  2. HDU.5985.Lucky Coins(概率DP)

    题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...

  3. HDU 4937 Lucky Number (数学,进制转换)

    题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator fi ...

  4. poj3519 Lucky Coins Sequence矩阵快速幂

    Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  5. Lucky Coins Sequence

    Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  6. HDU 5985 概率

    n种硬币各有cnt[i]枚,每轮下其有p[i]概率保留,问各种硬币只有它存活到最后一轮的概率. 设k轮后i硬币存活概率$a[i][k]=(1-p^k_i)^{cnt[i]}$ 则最后只有第i种硬币存活 ...

  7. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  8. HDU 5985/nowcoder 207D - Lucky Coins - [概率题]

    题目链接:https://www.nowcoder.com/acm/contest/207/D 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5985 ...

  9. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

随机推荐

  1. Go语言之函数签名

    使用type关键字进行, 函数类型变量也可以作为函数的参数或返回值. 我觉得属于高级技巧了,初学者可能需要很多代码实现的, 高级的就可以更通用的实现. package main import &quo ...

  2. 无法连接ssh,fatal: daemon() failed: No such device

    今天发现一个服务器的sshd无法启动,查看/var/log/secure里发现:fatal: daemon() failed: No such device 解决办法: rm /dev/null /d ...

  3. zookeeper 学习 状态机复制的共识算法

    https://www.youtube.com/watch?v=BhosKsE8up8 state machine replication 的 共识(consensus) 算法 根据CAP理论,一个分 ...

  4. 3. 深入研究 UCenter API 之 加密与解密(转载)

    1.  深入研究 UCenter API 之 开篇 (转载) 2.  深入研究 UCenter API 之 通讯原理(转载) 3.  深入研究 UCenter API 之 加密与解密(转载) 4.  ...

  5. 使用impala对kudu进行DML操作

    将数据插入 Kudu 表 impala 允许使用标准 SQL 语句将数据插入 Kudu 插入单个值 创建表: CREATE TABLE my_first_table ( id BIGINT, name ...

  6. 删除了原有的offset之后再次启动会报错park Streaming from Kafka has error numRecords must not ...

          笔者使用Spark streaming读取Kakfa中的数据,做进一步处理,用到了KafkaUtil的createDirectStream()方法:该方法不会自动保存topic parti ...

  7. window下面的守护进程,redis守护进程,RunHiddenConsole免费下载

    redis版本 在redis的目录下新建start_redis.bat 写入一下代码: D:/redis/RunHiddenConsole/RunHiddenConsole.exe redis-ser ...

  8. springmvc+ajax——第一讲(搭建)

    下面是整个整合测试的代码: ajax01.html TestController web.xml springmvc.xml applicationContext.xml <!DOCTYPE h ...

  9. 001 大数据情况下linux的配置

    一:配置的大纲 主要的配置有几个方面: 主机名 IP 网络映射 增加新用户 给新用户root的权限,方便实验 关闭防火墙 安全子系统需要关闭 二:主机名的配置 命令:vi /etc/sysconfig ...

  10. day 74ajax

    w3school地址ajax教程 ajax定义: AJAX(Asynchronous Javascript And XML) 翻译成中文就是'异步JavaScript和XML',也就是使用JavaSc ...