DZY Loves Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 856 Accepted Submission(s): 466
Problem Description
There are
n
black balls and
m
white balls in the big box.
Now, DZY starts to randomly pick out the balls one by one. It forms a sequence S . If at the i -th operation, DZY takes out the black ball, Si=1 , otherwise Si=0 .
DZY wants to know the expected times that '01' occurs in S .
Now, DZY starts to randomly pick out the balls one by one. It forms a sequence S . If at the i -th operation, DZY takes out the black ball, Si=1 , otherwise Si=0 .
DZY wants to know the expected times that '01' occurs in S .
Input
The input consists several test cases. (
TestCase≤150
)
The first line contains two integers, n , m(1≤n,m≤12)
The first line contains two integers, n , m(1≤n,m≤12)
Output
For each case, output the corresponding result, the format is
p/q
(
p
and
q
are coprime)
Sample Input
1 1 2 3
Sample Output
1/2 6/5HintCase 1: S='01' or S='10', so the expected times = 1/2 = 1/2 Case 2: S='00011' or S='00101' or S='00110' or S='01001' or S='01010' or S='01100' or S='10001' or S='10010' or S='10100' or S='11000', so the expected times = (1+2+1+2+2+1+1+1+1+0)/10 = 12/10 = 6/5
思路:最简单的思路就是暴力枚举出所有串,然后对a和b求一次gcd即可,n和m都是12,复杂度可以承受。
更加高效方便的办法是利用期望的可加性,把每一个位置上01串的期望相加即可得到总期望值
代码1:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long a;
int n,m;
void dfs(int s,int t,int d,int last,int num)
{
if(d==n+m)
{
a+=num;
return;
}
if(s<n) dfs(s+1,t,d+1,0,num);
if(t<m)
{
if(last==0) dfs(s,t+1,d+1,1,num+1);
else dfs(s,t+1,d+1,1,num);
}
}
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
a=0;
dfs(0,0,0,-1,0);
long long b=1;
for(long long i=n+m; i>=m+1; i--)
b*=i;
for(long long i=n; i; i--)
b/=i;
long long c=gcd(a,b);
printf("%lld/%lld\n",a/c,b/c);
}
return 0;
}
代码2:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long a;
int n,m;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
int a=n*m,b=n+m;
int c=gcd(a,b);
printf("%d/%d\n",a/c,b/c);
}
return 0;
}