跳过正文
  1. Posts/

codeforces 148 D. Bag of mice

·2 分钟
目录

http://codeforces.com/problemset/problem/148/D
#

题意:盒子里有w只白老鼠,b只黑老鼠,公主和魔王轮流取(公主先),先取到白老鼠的人获胜。魔王每次取完以后,盒子中的老鼠会因为吓尿了跑掉一只,跑掉的老鼠不算任何人取的。问公主获胜的概率。

思路:概率dp.. dp[i][j]表示有i只白老鼠,j只黑老鼠的时候公主获胜的概率。
#

转移方程
#

 11. 公主抽到白老鼠(之后龙不必再抽)  胜率为i/(i+j)*1
 22. 公主抽到黑老鼠,龙抽到黑老鼠,跳出一只黑老鼠,胜率为j/(i+j) * (j-1)/(i+j-1) * (j-2)/(i+j-2) * f[i][j-3] (j>=3)
 33. 公主抽到黑老鼠,龙抽到黑老鼠,跳出一只白老鼠,胜率为j/(i+j) * (j-1)/(i+j-1) * (i/(i+j-2) * f[i-1][j-2] (j>=2)
 44. 龙抽到白老鼠,胜率为0
 5
 6
 7
 8
 9
10
11
12
13
14/* ***********************************************
15Author :111qqz
16Created Time :2016年02月04日 星期四 02时44分23秒
17File Name :code/cf/problem/148D.cpp
18************************************************ */
19
20#include <cstdio>
21#include <cstring>
22#include <iostream>
23#include <algorithm>
24#include <vector>
25#include <queue>
26#include <set>
27#include <map>
28#include <string>
29#include <cmath>
30#include <cstdlib>
31#include <ctime>
32#define fst first
33#define sec second
34#define lson l,m,rt<<1
35#define rson m+1,r,rt<<1|1
36#define ms(a,x) memset(a,x,sizeof(a))
37typedef long long LL;
38#define pi pair < int ,int >
39#define MP make_pair
40
41using namespace std;
42const double eps = 1E-8;
43const int dx4[4]={1,0,0,-1};
44const int dy4[4]={0,-1,1,0};
45const int inf = 0x3f3f3f3f;
46const int N=1E3+7;
47int w,b;
48double dp[N][N];
49int main()
50{
51	#ifndef  ONLINE_JUDGE
52	freopen("code/in.txt","r",stdin);
53  #endif
54	cin>>w>>b;
55
56	ms(dp,0);
57	//dp[i][j]表示有i个白球,j个黑球的时候公主获胜的概率。
58	for ( int i = 1 ; i <= w ; i++) dp[i][0] = 1;
59	for ( int j = 1 ; j <= b ; j++) dp[0][j]= 0 ;
60
61	for ( int i = 1 ; i <= w ; i ++)
62	{
63	    for ( int j = 1 ; j <= b ; j++)
64	    {
65		double x = i*1.0;
66		double y = j*1.0;
67
68		dp[i][j]+=x/(x+y);
69		if (j>=3) dp[i][j] +=y/(x+y)*(y-1)/(x+y-1)*(y-2)/(x+y-2)*dp[i][j-3];
70
71		if (j>=2) dp[i][j] +=y/(x+y)*(y-1)/(x+y-1)*(x)/(x+y-2)*dp[i-1][j-2];
72	    }
73	}
74	printf("%.10f\n",dp[w][b]);
75
76  #ifndef ONLINE_JUDGE
77  fclose(stdin);
78  #endif
79    return 0;
80}

相关文章

codeforces 518 D. Ilya and Escalator

·1 分钟
http://codeforces.com/problemset/problem/518/D 题意:有n个人排队上一个电梯。。。在某一秒内,队首的人有p的概率上电梯,1-p的概率不动。每个人只有在队首的位置才可以上电梯(也就是每一秒内,最多只有一个人可以上电梯)。电梯无线长(也就是上了电梯就不会离开了),问在第t秒的时候,电梯上的人的个数的数学期望是多少。 # 思路:一开始推公式的我还是图样。这题是dp.其实也不难想。dp[i][j]表示第i秒时电梯上有j个人的概率。 当j==n的时候,也就是所以人都上了电梯以后。dp[i+1][j]+=dp[i][j],对于其他时刻 dp[i+1][j+1]+=dp[i][j]p,dp[i+1][j]+=dp[i][j](1-p). 初始化dp[0][0]=1,即0时刻电梯上有0个人的概率为1. # 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月02日 星期二 15时57分06秒 4File Name :code/cf/518D.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=2E3+7; 34int n,t; 35double p; 36double dp[N][N]; 37int main() 38{ 39 #ifndef ONLINE_JUDGE 40 freopen("code/in.txt","r",stdin); 41 #endif 42 ms(dp,0); 43 dp[0][0] = 1; 44 cin>>n>>p>>t; 45 for ( int i = 0 ; i <= t ; i++) 46 { 47 for ( int j = 0 ; j <= n ; j++) 48 { 49 if (j==n) 50 { 51 dp[i+1][j]+=dp[i][j]; 52 } 53 else 54 { 55 dp[i+1][j+1]+=dp[i][j]*p; 56 dp[i+1][j]+=dp[i][j]*(1-p); 57 } 58 } 59 } 60 double ans = 0 ; 61 for ( int j = 1 ; j <= n ; j++) 62 ans +=j*dp[t][j]; 63 64 printf("%.12f\n",ans); 65 66 #ifndef ONLINE_JUDGE 67 fclose(stdin); 68 #endif 69 return 0; 70}

codeforces 30 C. Shooting Gallery

·1 分钟
http://codeforces.com/contest/30/problem/C 题意:给出n个target在一个二维平面上。给出每个target的坐标,出现的时间,以及击中的概率。target出现之后就会瞬间消失,枪移动的单位速度为1,射击不需要时间。问能击中的target的最大期望是多少。

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放进去。