acm算法经典例题.pptx

  1. 1、本文档共90页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
一、数论 1: Wolf and Rabbit 描述 There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For example, m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes. 输入 The input starts with a positive integer P which indicates the number of test cases. Then on the following P lines,each line consists 2 positive integer m and n(0<m,n<2147483648). 输出 For each input m n, if safe holes exist, you should output "YES", else output "NO" in a single line. 样例输入 2 1 2 2 2 样例输出 NO YES 翻译: 描述 一座山有n 个洞,洞被标记为从 0 到n-1。兔子必须藏在一个洞中。狼会逆时针方向搜索兔子。狼一开始在洞 0,然后他会每m 个洞进 去一次。例如:m=2,n=6,狼就会依次进入洞 0 2 4 0。如果兔子藏在 1 3 5 就安全。 输入 第一行一个数字p,表示下面有p 组测试数据,每组测试数据 2 个数 m n(0<m,n<2147483648) 输出 每组数据输出一行,如果存在安全洞穴,输出"YES",否则输出"NO" 思路/方法:你是不是觉得总是无法通过,看看下面的解析 假设有n=6 个洞 0 1 2 3 4 5 当 m=4 时,狼进的洞是 0 4 2 0,也就是形成了一个循环,永远也到不了其他洞穴 当 m=5 时,狼进的洞是 0 5 4 3 2 1 0,这时所有的洞都遍历了一遍。 思考:当 m=4 和m=5 时,到底有什么区别? 当n 和m 有公约数(非 1)时,就会形成一个数字个数小于 n 的循环 当n 和m 无公约数时,就会形成一个数字个数为n 的循环,此时没有安全洞穴。 解题关键:这题就转化成了判断两个数是否有公约数。 代码: #include <iostream> using namespace std; long long greatestCommonDivisor(long long a, long long b)//最大公约数 { long long t; ; while(b) { t = a % b; a = b; b = t; } return a; } int main() { int i, p; long long m, n; cin >> p; for(i = 0; i < p; i++) { cin >> m >> n; if(greatestCommonDivisor(m, n) == 1)//公约数为 1 说明互斥,没有安全洞穴 cout << "NO\n"; else cout << "YES\n"; } return 0; } 2: a^b 描述 给定a 和b,输出a^b 的最后一个数字。 输入 输入数据有多组,每组数据占一行,每行为a 和b 的值(0<a,b<=2^30) 输出 对每组输入数据,输出 a^b

文档评论(0)

tiger02 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档