《ACM国际大学生程序设计竞赛题解Ⅰ》——模拟题

时间:2022-06-27 19:30:15

这篇文章来介绍一些模拟题,即一类按照题目要求将现实的操作转换成程序语言。

zoj1003:

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.

题目大意:给出两个整数n、m(假设m>n),请你判断是否存在一种方案,使得n = f1 * f2 *... , m=F1 * F2 *...,其中对于任意的i、j,有fi≠fj,fi≠Fj,fi∈[2,100]且fj∈[2,100]。

数理分析:其实对于题目大意的描述,笔者表述很抽象化,如果用文字描述,其实就是判断对于给出的两个整数n、m,进行因数分解(因子范围在1~100),能否得到两个完全不同的方案。假设我们已经得到了结果,我们先看看这个结果如何左右我们输出的结果。

如果存在这样一种方案,结合原文题意,这里驳回质疑,高分胜出,即输出m、n当中较高的那个。

如果不存在这样一种方案,则质疑成功,低分胜出,输出m、n中较小的那个。

这里注意到原文的一段话,“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.”这句话是说,如果m、n两者都没办法完成上述的因数分解,即用原文的话说是两个人都说谎,则不支持提出的质疑,按照原来的胜负态来处理,也就是高分或者获胜。

总结起来,如果想输出较小的数,当且仅当m、n在所以的因数分解中,n能够被因数分解但是m不能。而其余的情况都是输出较大数m。

搞清的如何输出,下面我们要解决的关键问题就是如何判断笔者在题目大意中描述的那个数学问题呢?

其实笔者感觉这道题放在模拟题中有点“干”,它其实涉及了数论的东西和dfs的思想(关于搜索后面会有一篇文章专门介绍)。

判断方法描述起来很简单,我们找到m、n所有的因数分解情况,然后按照给出的限制条件去判断即可。

但是如何巧妙的变成来实现这个过程呢?要先对m、n质因分解然后排列组合么?理论上可行但似乎有些繁琐。考虑到每个因子仅能出现一次的特点,我们利用dfs进行深搜,我们首先从枚举100开始枚举2~100这99个因子,一旦发现一个m或者n的因子pm或pn(假设当前找到了m的一个因子pm),那么我们可以将问题更加的微小化。即一开始,我们当前的问题是判断整数m、n是否能够得到两种不同的因数分解方案,因子范围是2~100,那么在找到了某个因子pm之后,我们的问题便可以缩小化成如下的等价形式,判断m/pm、n是否能够得到两种不同的因数分解方案,因子范围是2~pm-1,由此我们看到这递归的程序模式,其实如果熟悉欧几里得算法的读者,会更好的理解这个过程。

简单的参考代码如下。

#include<cstdio>
using namespace std;
bool atrue , btrue; int judge(int m,int n,int p)
{
if(atrue) return ;
if(n == && m == )
{
atrue = true;
btrue = true;
return ;
}
if(n == ) btrue = true; while(p > )
{
if(m%p == ) judge(m/p,n,p-);
if(n%p == ) judge(m,n/p,p-);
p--;
}
return ;
}
int main()
{
int a , b , temp;
while(scanf("%d%d",&a,&b) != EOF)
{
if(b > a)
{
temp = a;
a = b;
b = temp;
}
atrue = false;
btrue = false;
judge(a , b , ); if(!atrue && btrue)
printf("%d\n",b);
else
printf("%d\n",a);
}
}

《ACM国际大学生程序设计竞赛题解Ⅰ》——模拟题的更多相关文章

  1. 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题

    这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...

  2. 《ACM国际大学生程序设计竞赛题解I》——6&period;10

    Pku 1143: Description Christine and Matt are playing an exciting game they just invented: the Number ...

  3. 《ACM国际大学生程序设计竞赛题解I》——6&period;11

    pku 1107: Description Weird Wally's Wireless Widgets, Inc. manufactures an eclectic assortment of sm ...

  4. 《ACM国际大学生程序设计竞赛题解I》——6&period;8

    Poj1068: Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in ...

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会部分题解

    题目链接 2018 ACM 国际大学生程序设计竞赛上海大都会 下午午休起床被同学叫去打比赛233 然后已经过了2.5h了 先挑过得多的做了 .... A题 rand x*n 次点,每次judge一个点 ...

  6. 2018 ACM 国际大学生程序设计竞赛上海大都会赛

    传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...

  7. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  8. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it &lpar;扫描线&rpar;

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers &lpar;数位DP&rpar;

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. &lbrack;转载&rsqb;C&num;时间函数

    本文转自livedanta的博客的<C#时间函数> DateTime DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21 ...

  2. Intellij 导入play framework 项目

    新建一个项目 play new helloworld IshallbeThatIshallbe:~ iamthat$ mkdir temp IshallbeThatIshallbe:~ iamthat ...

  3. Mac OS恢复出厂系统方法

    1.重新启动时按住“Command()”和"R"键盘 2.选择磁盘工具

  4. zigbee智能家居基础扫盲

    zigbee Zigbee是基于IEEE802.15.4标准的低功耗个域网协议.根据这个协议规定的技术是一种短距离.低功耗的无线通信技术.这一名称来源于蜜蜂的八字舞,由于蜜蜂(bee)是靠飞翔和&qu ...

  5. ubuntu 解压rar

    Ubuntu下解压rar文件的方法 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤 ...

  6. 模拟jquery封装选择器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. &lbrack;KMP求最小循环节&rsqb;&lbrack;HDU1358&rsqb;&lbrack;Period&rsqb;

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

  8. Servlet基础(工作原理、生命周期)

    (一)Servlet开发与配置 1.1 开发步骤 1)编写java类,继承HttpServlet类 2)重新doGet和doPost方法 3)Servlet程序交给tomcat服务器运行! 配置信息: ...

  9. JavaScript 函数创建思想

    //定义一个函数的步骤//1.开辟一个新的空间地址//2.把函数体里面的代码当做字符串存储到空间里面(一个函数如果只定义了,没有执行的话,这个函数没有任何意义)//3.在把我们的地址给我们的函数名fu ...

  10. consul &amp&semi; registrator &amp&semi; consul-template 使用

    consul & registrator & consul-template 使用 参考这里的文章: https://www.jianshu.com/p/a4c04a3eeb57 do ...