博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 5194(DFS)
阅读量:4326 次
发布时间:2019-06-06

本文共 2140 字,大约阅读时间需要 7 分钟。

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. (
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
 

 

Source
 
题解:特判了下12 12以及 11 12 和 12 11..其余的话暴力枚举就行了..
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;int n,m,p,q;int ans[30];void dfs(int step,int a,int b){ if(a>m||b>n) return; ///剪枝,不然TLE if(a==m&&b==n){ q++; int k = 0; for(int i=0;i

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

#include
#include
#include
#include
#include
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+1,0,num); if(a<=n){ if(pre==0) dfs(a+1,b,1,num+1); else dfs(a+1,b,1,num); }}int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ p = q = 0; dfs(0,0,-1,0); int d = gcd(p,q); printf("%d/%d\n",p/d,q/d); } return 0;}

 

转载于:https://www.cnblogs.com/liyinggang/p/5702724.html

你可能感兴趣的文章
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>