【ZOJ1003】Crashing Balloon(DFS)
Crashing Balloon
Time Limit: 2 Seconds Memory Limit: 65536 KB
On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV. The rule is very simple. On the ground there are 100 labeled balloons, with the numbers 1 to 100. After the referee shouts "Let's go!" the two players, who each starts with a score of "1", race to crash the balloons by their feet and, at the same time, multiply their scores by the numbers written on the balloons they crash. After a minute, the little audiences are allowed to take the remaining balloons away, and each contestant reports his\her score, the product of the numbers on the balloons he\she's crashed. The unofficial winner is the player who announced the highest score.
Inevitably, though, disputes arise, and so the official winner is not determined until the disputes are resolved. The player who claims the lower score is entitled to challenge his\her opponent's score. The player with the lower score is presumed to have told the truth, because if he\she were to lie about his\her score, he\she would surely come up with a bigger better lie. The challenge is upheld if the player with the higher score has a score that cannot be achieved with balloons not crashed by the challenging player. So, if the challenge is successful, the player claiming the lower score wins.
So, for example, if one player claims 343 points and the other claims 49, then clearly the first player is lying; the only way to score 343 is by crashing balloons labeled 7 and 49, and the only way to score 49 is by crashing a balloon labeled 49. Since each of two scores requires crashing the balloon labeled 49, the one claiming 343 points is presumed to be lying.
On the other hand, if one player claims 162 points and the other claims 81, it is possible for both to be telling the truth (e.g. one crashes balloons 2, 3 and 27, while the other crashes balloon 81), so the challenge would not be upheld.
By the way, if the challenger made a mistake on calculating his/her score, then the challenge would not be upheld. For example, if one player claims 10001 points and the other claims 10003, then clearly none of them are telling the truth. In this case, the challenge would not be upheld.
Unfortunately, anyone who is willing to referee a game of crashing balloon is likely to get over-excited in the hot atmosphere that he\she could not reasonably be expected to perform the intricate calculations that refereeing requires. Hence the need for you, sober programmer, to provide a software solution.
Input
Pairs of unequal, positive numbers, with each pair on a single line, that are claimed scores from a game of crashing balloon.
Output
Numbers, one to a line, that are the winning scores, assuming that the player with the lower score always challenges the outcome.
Sample Input
343 49
3599 610
62 36
Sample Output
49
610
62
题意:地面上有100个气球 编号为1~100 两个人踩气球 初始分数都是1 踩到相应编号的气球则分数乘以相应编号 最后两个人报出自己的分数 分数高的取胜 但是可能存在有人说谎的情况 分数低的人可以质疑分数高的人 如果分数高的人有一个分数不是自己踩气球得到的 则质疑是对的 分数低的选手是赢家 例如 分数高的选手要得到他说出的分数必须要踩到分数低的选手一定会踩到的气球 则质疑成功 另外 如果两个人分数都计算错误的话 则质疑被否决
题解:根据题目要求模拟 首先将分数高的赋值n 分数低的赋值m 之后通过递归因式分解 判断是否有公因子 因为气球编号是1~100 所以从100开始向下判断因子就好了 如果有公因子 分数低的选手是赢家 如果没有 分数高的选手是赢家 如果全部说谎或者全部正确 则判断高分获胜
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; bool aT, bT; void dfs(int n, int m, int p); int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
if(n < m){ //将分数高的赋值n 分数低的赋值m
swap(n, m);
} aT = false; //先将a b都赋值为说谎
bT = false; dfs(n, m, ); //递归因式分解 if(!aT && bT){ //只有高分的说谎 则输出低分
printf("%d\n", m);
}
else{ //高分的没有说谎或者全部说谎或者全部正确 则输出高分
printf("%d\n", n);
}
}
return ;
} void dfs(int n, int m, int p){
if(n == && m == ){ //两个人都没有说谎 将aT赋值为没有说谎就好了
aT = true;
return ;
}
if(m == ){ //低分者没有说谎
bT = true;
} while(p > ){ //因式分解
if(n % p == ){
dfs(n/p, m, p-);
}
if(m % p == ){
dfs(n, m/p, p-);
} --p;
}
}
Alpha
【ZOJ1003】Crashing Balloon(DFS)的更多相关文章
- 【算法】深度优先搜索(dfs)
突然发现机房里有很多人不会暴搜(dfs),所以写一篇他们能听得懂的博客(大概?) PS:万能 yuechi ---- 大法师怎么能不会呢?! 若有错误,请 dalao 指出. 前置 我知道即使很多人都 ...
- 【搜索】棋盘问题(DFS)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- 【算法】深度优先搜索(DFS)III
1. DFS生成排列 众所周知,1,2…n的排列一共有n!个,因此生成全排列至少需要n!的时间复杂度.如果用循环来生成排列,当n稍大时,内外循环会非常之多.可以用DFS模拟解决,生成0 … n-1的排 ...
- 【UVa】208 Firetruck(dfs)
题目 题目 分析 一开始不信lrj的话,没判联通,果然T了. 没必要全部跑一遍判,只需要判断一下有没有点与n联通,邻接表不太好判,但无向图可以转换成去判n与什么联通. 关于为什么要判,还是因为 ...
- 【HDOJ6628】permutation 1(dfs)
题意:求1到n的排列中使得其差分序列的字典序为第k大的原排列 n<=20,k<=1e4 思路:爆搜差分序列,dfs时候用上界和下界剪枝 #include<bits/stdc++.h& ...
- 【Luogu】P3761城市(dfs)
题目链接 emmm我思维好水…… 想了一会lct发现好像不对,然后开始转DP稍微有一点思路,然后看了题解…… 首先可以枚举边,然后原树被你拆成了两个子树. 设D1D2是两个子树的直径,W1W2是子树内 ...
- CJOJ 1071 【Uva】硬币问题(动态规划)
CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...
- 【Luogu1273】有线电视网(动态规划)
[Luogu1273]有线电视网(动态规划) 题面 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端, ...
- 【Luogu2458】保安站岗(动态规划)
[Luogu2458]保安站岗(动态规划) 题面 题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地 ...
随机推荐
- MVC与webservice上传文件(图片和视频),希望帮且到一些朋友
最近做一个项目,要把图片和视频传到服务器上(网站与图片服务器分开),在网上找了好久,没找到完整的资料. 自己也折腾了半天,才把完整的代码实现完.可能好多朋友都有实现过,没分享代码吧,写得不好希望不要见 ...
- Android数据持久化技术 — — —SharedPreferences
SharedPreferences是使用键值对的方式来存储数据. 要想使用SharedPreferences来存储数据,必须获取SharedPreferences对象,获取SharedPreferen ...
- 自定义属性的时候,尽量不要使用value这个命名
最近我在重写select下拉组件时,使用ul->li来模拟select中的一个个option,并给li添加索引,取名为value. 非IE浏览器下value值工作正常,但是IE下value值工作 ...
- Caliburn.Micro学习笔记目录——li-peng
Caliburn.Micro学习笔记(一)----引导类和命名匹配规则 Caliburn.Micro学习笔记(二)----Actions Caliburn.Micro学习笔记(三)----事件聚合IE ...
- PyCharm 3.4.1注册码
PyCharm 3.4下载地址:http://www.jetbrains.com/pycharm/download/index.html PyCharm 3.0 注册码 PyCharm3 序列号 Li ...
- 如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可
如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架).而若要指定某几个角(小于4)为圆角而别的不变时 ...
- 怎么在excel中快速查找重复记录
假设数字在A列,数字从A1开始:方法一:辅助列中输入公式,数据重复时会出现“重复”提示.=IF(COUNTIF(A:A,A1)>1,"重复","") ,下 ...
- Qt for Android开发Android应用时的各种错误汇总(此片博文不成熟,请大家略过)
“Qt for Android真的很脆弱,项目能跑起来靠的是奇迹,跑不起来,各种报错才是正常...” 问题一:Qt for Android编译不过:make (e=2): 系统找不到指定的文件. 之前 ...
- 聊一聊jquery文件上传(支持多文件上传)
谈到文件上传,现在一般都用现成的组件可以使用.PC端的可以使用uploadify.针对微网站H5也有uploadifive.但是这组件并不能满足各种场景的需求,例如:预览 切图 放大缩小,取消之类的. ...
- 日志:using the Connector/J connection property 'autoReconnect=true' to avoid this problem
com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was581 ...