跳过正文
  1. Posts/

hdu 4336 Card Collector (2012多校 #4) (容斥原理模板题)

·2 分钟

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

题意:有n种卡片,买一包干脆面得到第i种卡片的概率是p[i],每包干脆面最多有一张卡片,问收集齐所有卡片要买的干脆面的包数的数学期望。

思路:容斥模板题。1.0/p[i]就是拿到某张卡片需要买的包数的数学期望

注意体会这种具体应用容斥的模拟方法,把1«n转化成二进制来模拟有1个元素的集合,有2个元素的集合…有n个元素的集合。 核心代码:

  1 for ( int msk = 1 ; msk <(1<<n) ; msk++)
  2	    {
  3		double res = 0.0;
  4		int bits  = 0;
  5		for ( int i = 0 ; i < n ; i++)
  6		{
  7		  //  cout<<"msk:"<<msk<<" "<<(1<<i)<<endl;
  8		    if (msk&(1<<i))
  9		    {
 10			bits++;
 11			res +=p[i];
 12		    }
 13		}
 14		if (bits%2==1)
 15		{
 16		    ans += 1.0/res;
 17		}
 18		else
 19		{
 20		    ans -= 1.0/res;
 21		}
 22	    }
 23
 24
 25
 26
 27
 28
 29
 30
 31/* ***********************************************
 32Author :111qqz
 33Created Time :2016年02月29日 星期一 18时39分23秒
 34File Name :code/hdu/4336.cpp
 35************************************************ */
 36
 37#include <cstdio>
 38#include <cstring>
 39#include <iostream>
 40#include <algorithm>
 41#include <vector>
 42#include <queue>
 43#include <set>
 44#include <map>
 45#include <string>
 46#include <cmath>
 47#include <cstdlib>
 48#include <ctime>
 49#define fst first
 50#define sec second
 51#define lson l,m,rt<<1
 52#define rson m+1,r,rt<<1|1
 53#define ms(a,x) memset(a,x,sizeof(a))
 54typedef long long LL;
 55#define pi pair < int ,int >
 56#define MP make_pair
 57
 58using namespace std;
 59const double eps = 1E-8;
 60const int dx4[4]={1,0,0,-1};
 61const int dy4[4]={0,-1,1,0};
 62const int inf = 0x3f3f3f3f;
 63const int N=25;
 64double p[N];
 65int n;
 66int main()
 67{
 68	#ifndef  ONLINE_JUDGE
 69	freopen("code/in.txt","r",stdin);
 70  #endif
 71
 72	while (~scanf("%d",&n))
 73	{
 74	    ms(p,0);
 75	    double ans = 0 ;
 76	    for ( int i = 0 ; i < n ; i++) scanf("%lf",&p[i]);
 77
 78	    for ( int msk = 1 ; msk <(1<<n) ; msk++)
 79	    {
 80		double res = 0.0;
 81		int bits  = 0;
 82		for ( int i = 0 ; i < n ; i++)
 83		{
 84		  //  cout<<"msk:"<<msk<<" "<<(1<<i)<<endl;
 85		    if (msk&(1<<i))
 86		    {
 87			bits++;
 88			res +=p[i];
 89		    }
 90		}
 91		if (bits%2==1)
 92		{
 93		    ans += 1.0/res;
 94		}
 95		else
 96		{
 97		    ans -= 1.0/res;
 98		}
 99	    }
100
101	    printf("%.8f\n",ans);
102	}
103
104  #ifndef ONLINE_JUDGE
105  fclose(stdin);
106  #endif
107    return 0;
108}

20160305update:前几天有道题没写递归形式tle掉了。。。所以来学习一下容斥原理的递归形式。

1dfs(int beg,set S,int sym)
2{
3     ans+=num(S)*sym;
4     for(int i=beg;i<=n;i++)
5         dfs(i,SA[i],sym*-1);
6}
7
8for(int i=1;i<=n;i++)
9     dfs(i,A[i],1);

以及这道题用递归的形式写了一遍。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年03月05日 星期六 15时02分43秒
 4File Name :code/hdu/r4336.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=25;
34double p[N];
35int n;
36double ans;
37
38void  dfs( int id,double x,int flag)   //flag表示当前是要加还是减..和元素个数的奇偶性相关(奇数+,偶数-)
39{
40    ans +=(1.0/x)*flag;
41 //   cout<<"id:"<<id<<" x:"<<x<<" flag:"<<flag<<" ans:"<<ans<<endl;
42    for ( int i = id+1 ; i < n ; i++)
43	dfs(i,x+p[i],flag*-1);
44}
45int main()
46{
47	#ifndef  ONLINE_JUDGE
48	freopen("code/in.txt","r",stdin);
49  #endif
50
51	while (~scanf("%d",&n))
52	{
53	    ms(p,0);
54	    ans = 0 ;
55	    for ( int i = 0 ; i <  n; i++) scanf("%lf",&p[i]);
56
57
58	    for ( int i = 0; i < n ; i++)
59	    {
60		dfs(i,p[i],1);
61	    }
62
63	    printf("%.8f\n",ans);
64	}
65
66  #ifndef ONLINE_JUDGE
67  fclose(stdin);
68  #endif
69    return 0;
70}

相关文章

codeforces 107 B. Basketball Team

·2 分钟
http://codeforces.com/problemset/problem/107/B 题意:有m个部门,每个部分s[i]个人,HW在第h部门,现在要从这m个部门中挑选包括HW在内的n个人去参加比赛,问被挑选的人中有HW的队友(同部门的人)的概率是多少。如果m个部分的人数不够组成n人的球队,输出-1. # 思路:考虑一般情况。至少有一个队友的情况较多,应该从反面考虑,即没有一个队友的情况。选完HW以后面临的状态是:事件总数为从total(m个部门的人员之和)-1个人中选n-1个的方案数,包含的事件数目为从a(a=total-s[h])中选n-1个人包含的方案数。 可以看出分母相同,可以约掉。 # 然后对于边界情况,首先判断total是否比n小。然后,如果a<n-1,表示除去HW所在的h部分之外的人不可能组成n-1个人,也就是一定要选择HW的队友,概率为1.

codeforces 312 B. Archer

·2 分钟
http://codeforces.com/problemset/problem/312/B # 题意:两个人比赛射箭,先射的人射中的概率是a/b,后射的人射中的概率是c/d,问先射的人赢的概率。 思路:应该叫条件概率。。。? 不过我们可以用古典概型的思维想。每射一次看成一个点,射中的点用白色表示,没有射中的用黑色表示。如果两个人第i次都没有射中,那么就要继续第i+1 轮,而第i+1轮和之前的每一轮是独立的。等于重复这个过程。所以古典概型的样本总量应该减去宝石两个人都没有射中的点的个数,为bd-(b-a)(d-c),整理为bc+ad-a*c,设为n.要想第一个人赢,那么对于某一次,只要不是第一个人没射中,第二个人射中这种情况,就都是第一个人赢。而第一个人没射中的事件数为b-a,第二个人射中的事件数为c,总数为(b-a)*c,所以答案为(n-(b-a)*c)/n

codeforces 453 A. Little Pony and Expected Maximum

·1 分钟
http://codeforces.com/problemset/problem/453/A 题意:m面筛子,每面点数出现的概率相同,连续投掷n次,问出现的最大值的数学期望。 思路:手写样例。。。发现答案为 。。。记得把(1/m)^n放进去。

codeforces 476 B. Dreamoon and WiFi

·2 分钟
http://codeforces.com/problemset/problem/476/B 题意:给出两个长度相等-且不超过10的字符串,串1只包含‘-’,’+‘。按照‘+’为1,‘-’为-1累加可以得到一个值。串2还包含若干‘?’,代表该处的值不确定,且为’+‘和’-‘的概率相等,都是0.5.问串2的值和串1相等的概率。 思路:我们可以扫一遍得到‘?’的个数和两个式子的差值。设问号个数为a,差值为b,那么在a个问号中需要有(a-b)/2个为‘+’(容易知道,a,b一定奇偶性相同,所以a-b一定能被2整除),根据超几何分布,概率为 c[a][(a-b)/2]*(1/2)^a; 写的时候可以先打个组合数的表。1A,开心。

codeforces #341 div2 C. Wet Shark and Flowers

·3 分钟
http://codeforces.com/contest/621/problem/C C. Wet Shark and Flowers time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.