【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]保安站岗(动态规划) 题面 题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地 ...
随机推荐
- 当你刷新当前Table时,刷新后如何回到你上一次所在位置呢?
第一: 在你刷新前保存所在位置的行号 procedure XXXClass.LockPositionEx;begin DisableControls; FHistoryRecNo := 0; FHis ...
- 布局容器layout Container
画布canvas 盒子Box VBox Hbox-->HGroup VGroup 控制条 ControlBar
- MongoDB学习
最近在学习,参考一线码农的教程 http://www.cnblogs.com/huangxincheng/category/355399.html
- 转载--提高C++性能的编程技术
读书笔记:提高C++性能的编程技术 第1章 跟踪范例 1.1 关注点 本章引入的实际问题为:定义一个简单的Trace类,将当前函数名输出到日志文件中.Trace对象会带来一定的开销,因此在默认情况 ...
- nosql->redis学习 数据类型
redis->string 二进制 setnx name lijie 判断键值 是否存在 如果存在返回0 不存在 吧值设置进去 setex 指定键值有效期时间 setex name ...
- HSLA颜色
CSS2中色彩模式只有RGB色彩模式(RGB即RED.Green.BLue)和十六进制模式,为了能支持 透明opacity 的Alpha值,CSS3又增加了RGBA色彩模式(RGBA即RED.Gree ...
- iOS内置音频
Predefined soundsThere are some predefined system sounds, for the system sound ID in the range 1000 ...
- 1、MVC和EF中的 Model First 和 Code First
准备:先引入MVC和EF的dll包 *命令方法:打开工具——库程序包管理器——程序包管理器控制台,选择自己的项目 a) Install-Package EntityFramework -Ver ...
- Ubuntu 14.04中安装最新版Eclipse
Ubuntu 14.04中安装最新版Eclipse 来源:Linux社区 作者:Linux 1.安装OpenJDK Java 7 如果你的系统中没有安装Java,我们需要按照如下步骤事先安装好 ...
- 获得触发hover事件的元素id
例: <div class="menu"> <ul> <li> <a id="menu1"></a> ...