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 ...
随机推荐
- scrapy使用记录
1 进入pip安装目录 python -m pip install --upgrade pip pip install Scrapy 2. 创建一个项目 scrapy startproject tes ...
- 【bzoj4152】[AMPPZ2014]The Captain 堆优化Dijkstra
题目描述 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入 第一行包含一个正整数n(2<=n< ...
- P1531 I Hate It
题目背景 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 题目描述 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的 ...
- 2018牛客多校第一场 B.Symmetric Matrix
题意: 构造一个n*n的矩阵,使得Ai,i = 0,Ai,j = Aj,i,Ai,1+Ai,2+...+Ai,n = 2.求种类数. 题解: 把构造的矩阵当成邻接矩阵考虑. 那么所有点的度数都为2,且 ...
- Linux产生背景
By francis_hao Oct 26,2016 很久很久以前,大概在1965年左右,由贝尔实验室(Bell).麻省理工学院(MIT)及通用电气公司(GE)共同发起了一个叫做Multics的项目, ...
- zigbee ---- profile 和 cluster
在zigbee规范中,引入了profile, cluster的概念.具体说来,假设规范一个profile(可以理解成一套规定),这个profile用来规范智能家居领域的相关产品都要满足那些要求,那么h ...
- QT QLayout 清空
QLayout* pLayout = (QLayout*)ui->frame->layout(); )) { QWidget* pWidget = child->widget(); ...
- idea讲web项目部署到tomcat,热部署
idea是自动保存文件的,不需要ctrl+s手动保存. idea使用不习惯,修改了jsp文件后,刷新浏览器并没有立刻显示出来,而是要重新编译一下代码,重新部署才会出现. 在idea tomcat 中s ...
- BZOJ 3994: [SDOI2015]约数个数和
3994: [SDOI2015]约数个数和 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 898 Solved: 619[Submit][Statu ...
- 【hdu2222-Keywords Search】AC自动机基础裸题
http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...