BestCoder Round #35(DZY Loves Balls-暴力dp)

DZY Loves Balls

Accepts: 371
Submissions: 988
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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, S i =1  , otherwise S i =0  .

DZY wants to know the expected times that '01' occurs in S  .

Input

The input consists several test cases. ( TestCase150  )

The first line contains two integers, n  , m(1n,m12) 

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/5


    
    
Hint
Case 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

这次学乖,开始DP乱搞

其实还有更简单的敲公式法,,不过我就直接把打表程序交了

next_permunation 这种暴力也是可以的??


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100) 
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
ll gcd(ll a,ll b){
	if (b==0) return a;
	return gcd(b,a%b);
}
class fenshu
{
public:
	ll a,b; //分母a 分子b 
	fenshu(ll _b,ll _a):a(_a),b(_b){}
	fenshu():a(1),b(0){}
	void relax()
	{
		int t=gcd(a,b);
		a/=t,b/=t;
	}
	friend fenshu operator*(fenshu a,fenshu b){fenshu c=fenshu(a.b*b.b,a.a*b.a);c.relax();return c;}
	friend fenshu operator+(fenshu a,fenshu b){fenshu c=fenshu(a.a*b.b+a.b*b.a,a.a*b.a);c.relax();return c;}
	void pri()
	{
		cout<<b<<'/'<<a;
	}
}f[MAXN][MAXN][2];
int n,m;
void turn_out(int n,int m)
{
	fenshu t=f[n][m][0]*fenshu(m,n)+f[n][m][1]*fenshu(n-m,n); 
	t.pri();

} 
int main()
{
//	freopen("a.in","r",stdin);
	
	n=24,m=12;
	
	Fork(i,2,n)
		Rep(j,i+1)
		{
			int k=0;
			f[i][j][k]=f[i-1][j-1][0]*fenshu(j-1,i-1)+f[i-1][j-1][1]*fenshu(i-j,i-1);
			k=1;
			f[i][j][k]=(fenshu(1,1)+f[i-1][j][0])*fenshu(j,i-1)+f[i-1][j][1]*fenshu(i-1-j,i-1);
		}
/*
	For(i,24)
	{
		Rep(j,min(i+1,13))
		{
			turn_out(i,j);cout<<' ';
		}	
		cout<<endl;	
	}
*/
	while(scanf("%d%d",&n,&m)==2)
	{
		n+=m;
		fenshu t=f[n][m][0]*fenshu(m,n)+f[n][m][1]*fenshu(n-m,n); 
		t.pri();cout<<endl;
	}
	
	
	return 0;
}







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值