zoukankan      html  css  js  c++  java
  • HDU 4336:Card Collector(容斥原理)

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

    Card Collector

    Special Judge

    Problem Description
     
    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award. 

    As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.
     
    Input
     
    The first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= 1), indicating the possibility of each card to appear in a bag of snacks.

    Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
     
    Output
     
    Output one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.

    You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
     
    Sample Input
     
    1
    0.1
    2
    0.1 0.4
     
    Sample Output
     
    10.000
    10.500
     
    题意:要收集N张卡,吃一包方便面得到第i张卡的概率为p[i],问收集N张卡吃的方便面包数的期望。
    思路:容斥原理。奇数加偶数减。
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 using namespace std;
     6 
     7 double p[22];
     8 double dp[22];
     9 
    10 int main()
    11 {
    12     int n;
    13     while(~scanf("%d", &n)) {
    14         for(int i = 0; i < n; i++)
    15             scanf("%lf", &p[i]);
    16         double ans = 0;
    17         double sum;
    18         int cnt;
    19         for(int i = 1; i < (1 << n); i++) {
    20             sum = 0; cnt = 0;
    21             for(int j = 0; j < n; j++) {
    22                 if(i & (1 << j)) {
    23                     cnt++;
    24                     sum += p[j];
    25                 }
    26             }
    27             if(cnt & 1) ans += 1.0 / sum;
    28             else ans -= 1.0 / sum;
    29         }
    30         printf("%f
    ", ans);
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    Qt编程规范
    QtZint编译过程记录(要使用 QTMAKE_CFLAGS += /TP 参数)
    中科同向备份软件Heartsone-backup(足足16个软件,可差异化备份虚拟机)
    单例(Singleton)模式
    COFF/PE文件结构
    模板样式优化
    如何测试网页的登录页面
    enode框架step by step之消息队列的设计思路
    服务承载
    CSS文件和Javascript文件的压缩
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5794791.html
Copyright © 2011-2022 走看看