DZY Loves Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 860    Accepted Submission(s): 467

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.

 
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

 
Source
 
题解:特判了下12 12以及 11 12 和 12 11..其余的话暴力枚举就行了..
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
int n,m,p,q;
int ans[];
void dfs(int step,int a,int b){
if(a>m||b>n) return; ///剪枝,不然TLE
if(a==m&&b==n){
q++;
int k = ;
for(int i=;i<n+m-;i++){
if(ans[i]==&&ans[i+]==){
p++;
}
}
return;
}
for(int i=;i<=;i++){
if(a<=m&&i==){
ans[step] = i;
dfs(step+,a+,b);
}
if(b<=n&&i==){
ans[step] = i;
dfs(step+,a,b+);
}
}
}
int gcd(int a,int b){
return b==?a:gcd(b,a%b);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
if(n==&&m==){
printf("6/1\n");
continue;
}
if(n==&&m==||n==&&m==){
printf("132/23\n");
continue;
}
p = q = ;
dfs(,,);
int d = gcd(p,q);
printf("%d/%d\n",p/d,q/d);
}
return ;
}

改成了标记前结点,快了许多。

#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
int n,m,p,q;
void dfs(int a,int b,int pre,int num){
if(a>n||b>m) return;
if(a==n&&b==m){
p+=num;
q++;
return;
}
if(b<=m) dfs(a,b+,,num);
if(a<=n){
if(pre==) dfs(a+,b,,num+);
else dfs(a+,b,,num);
}
}
int gcd(int a,int b){
return b==?a:gcd(b,a%b);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
p = q = ;
dfs(,,-,);
int d = gcd(p,q);
printf("%d/%d\n",p/d,q/d);
}
return ;
}

hdu 5194(DFS)的更多相关文章

  1. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  2. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  3. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  4. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  5. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  6. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  7. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  8. hdu 1716(dfs)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716     排列2   Problem Description Ray又对数字的列产生了兴趣:现 ...

  9. hdu 4705 dfs统计更新节点信息

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 #pragma comment(linker, "/STACK:16777216&qu ...

随机推荐

  1. ArcGIS Server中创建的两个账户有什么区别

    新手常常有这样的疑问: 在安装ArcGIS Server的时候创建的账户和在ArcGIS Server Manager上面创建的账户有什么区别? 解答:前者是是为ArcGIS Server创建的操作系 ...

  2. SSH Secure Shell Client的使用方法

    1:双击其客户端图标,出现下图所示窗体 2:我使用她主要用于发布项目的,所以第一次使用会选择新建一个账户 3:填写账户的名称 4:完善账户的信息 5:主要用填下远程主机的IP/USER/PORT,在需 ...

  3. 使用ExecutorService实现线程池

    ExecutorService是java提供的用于管理线程池的类. 线程池的作用: - 控制线程数量 - 重用线程 当一个程序中创建了许多线程,并在任务结束后销毁,会给系统带来过度消耗资源,以及过度切 ...

  4. float与定位的区别

    float和绝对定位的区别 CSS中脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离文档流的元素不存在而进行定位.1 需要注意的是,使用float脱离文档流时,其他盒子 ...

  5. 算法学习——kruskal重构树

    kruskal重构树是一个比较冷门的数据结构. 其实可以看做一种最小生成树的表现形式. 在普通的kruskal中,如果一条边连接了在2个不同集合中的点的话,我们将合并这2个点所在集合. 而在krusk ...

  6. Spring AOP前置通知实例讲解与AOP详细解析

    一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Service.他们都有sayHello():我们的需求是在调用这两个方法之前,要先完成写日志的 ...

  7. error C3872: '0x3000': this character is not allowed in an identifier 解决方法

    文章参考地址:http://blog.csdn.net/danxuezx/article/details/5096497 编译时遇到这个错误多半是从网上拷贝一段代码到VS里然后编译时产生的,这是因为拷 ...

  8. 停课day5

    一转眼,已经停课五天了. 高二大佬们已经都走了,在机房里面呆着,有时感觉很孤寂. 但是为了能学好竞赛,这些都是在所不惜的. 好像多打打比赛啊,可是cf要FQ,洛谷之类的比赛还不勤. 哎,先去学一发SP ...

  9. ng依赖注入

    依赖注入 1.注入器在组件的构造函数中写服务constructor(private httpreq:HttpService) { } 2.提供器 providers: [HttpService],

  10. 转:Nginx国人开发缩略图模块(ngx_image_thumb)

    ngx_image_thumb是nginx中用来生成缩略图的模块,生存缩略图的方法很多,之前也写过一篇 <nginx生成缩略图配置>,在github上发现国人开发的一款模块,作者的文档写的 ...