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. (TestCase≤150 )

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/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;
}
时间: 2024-08-01 11:32:40

BestCoder Round #35(DZY Loves Balls-暴力dp)的相关文章

BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 323    Accepted Submission(s): 86 Problem Description A topological sort or topological ordering of a directed gr

HDU-5194-DZY Loves Balls(BestCoder Round # 35 )

DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 374    Accepted Submission(s): 205 Problem Description There are n black balls and m white balls in the big box. Now, DZY starts

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

Hdoj 5194 DZY Loves Balls 【打表】+【STL】

DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 394    Accepted Submission(s): 221 Problem Description There are n black balls and m white balls in the big box. Now, DZY starts

hdu5194 DZY Loves Balls 【概率论 or 搜索】

//yy:那天考完概率论,上网无聊搜个期望可加性就搜到这题,看到以后感觉特别亲和,挺有意思的. hdu5194 DZY Loves Balls [概率论 or 搜索] 题意: 一个盒子里有n个黑球和m个白球[n,m≤12].每次随机从盒子里取走一个球,取了n+m次后,刚好取完.现在用一种方法生成了一个随机的01串S[1-(n+m)],如果第i次取出的球是黑色的,那么S[i]=1,如果是白色的,那么S[i]=0.求'01'在S串中出现的期望次数. 题解: 求出在第i个位置上出现0,第i+1个位置上

Codeforces Round #254 DZY Loves Colors

题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0. 有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i个位置未更新前颜色为color[i])的colorfulness增加|color[i] -x|; 另一种操作是询问一段区间[L,R]输出该区间的colorfulness总和. 1 #include <iostream> 2 #include <cstdio> 3 #include <cst

[HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP)

[HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP) 题面 有三个人从一张N个点无重边的有向无环图上的三个点出发,每单位时间,他们分别选择当前点的一条出边走下去.有向无环图点有点权,任意时刻他们所在的三个点两两点权相差不超过K.他们可以在任意三个点同时结束.求合法的路径总数.N≤50. 分析 暴力的做法,设\(dp[i][j][k]\)表示第一个人在i,第二个人在j,第三个人在k的方案数,然后枚举三个人接着到的地方x,y,z,倒推\(dp

hdu-5645 DZY Loves Balls(水题)

题目链接: DZY Loves Balls Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 105    Accepted Submission(s): 75 Problem Description DZY loves playing balls. He has n balls in a big box. On each ball

BestCoder Round #75 King&amp;#39;s Order dp:数位dp

King's Order Accepts: 381 Submissions: 1361 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description After the king's speech , everyone is encouraged. But the war is not over. The king needs to give orders