hdu 5194(DFS)
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
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.
The first line contains two integers, n, m(1≤n,m≤12)
2 3
6/5
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
#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)的更多相关文章
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- hdu 1716(dfs)
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Problem Description Ray又对数字的列产生了兴趣:现 ...
- hdu 4705 dfs统计更新节点信息
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 #pragma comment(linker, "/STACK:16777216&qu ...
随机推荐
- lintcode-113-删除排序链表中的重复数字 II
113-删除排序链表中的重复数字 II 给定一个排序链表,删除所有重复的元素只留下原链表中没有重复的元素. 样例 给出 1->2->3->3->4->4->5-&g ...
- WebStorm中配置ExtJS
原文链接:http://zhidao.baidu.com/link?url=yX0wDWrL-b2P8k3JNNI38Fb6keuAgm0j9E-QBL1KfWXrZgLZ88grAOVJvat6dJ ...
- BZOJ4544 椭圆上的整点(数论)
https://www.cnblogs.com/Gloid/p/9538413.html 基本思路没有太大差别.得到2n=d(a2+3b2),其中d=gcd(n-x,n+x),n-x==a2& ...
- JAVA 异常处理的认知学习过程
没有异常处理 学生时代,我编写的java代码中,很少会有try catch.最主要的原因如下: 应用的规模很小 没有不确定因素 代码可控性高 如果规模小,往往就没有复杂的逻辑链路,整个软件的分层也很浅 ...
- [Leetcode] set matrix zeroes 矩阵置零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...
- [Leetcode] scramble string 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 微信小程序使用Socket
首先,一个小程序同时只能有一个WebSocket连接,如果当前已经存在一个WebSocket连接,会关闭当前连接,并重新建立一个连接. 其次,如果使用了appID,协议必须是 wss://... 最近 ...
- Out of memory due to hash maps used in map-side aggregation解决办法
在运行一个group by的sql时,抛出以下错误信息: Task with the most failures(4): -----Task ID: task_201411191723_723592 ...
- Hbase写入量大导致region过大无法split问题
最近在线上往hbase导数据,因为hbase写入能力比较强,没有太在意写的问题.让业务方进行历史数据的导入操作,中间发现一个问题,写入速度太快,并且业务数据集中到其中一个region,这个region ...
- 转:A Painless Q-learning Tutorial (一个 Q-learning 算法的简明教程)
demo 参见 MDP DEMO 本文是对 http://mnemstudio.org/path-finding-q-learning-tutorial.htm 的翻译,共分两部分,第一部分为中文 ...