2024年5月11日发(作者:)
一、数论
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 输出 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 输出 每组数据输出一行,如果存在安全洞穴,输出"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 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 << "NOn"; else cout << "YESn"; } return 0; } 2: a^b 描述 给定a和b,输出a^b的最后一个数字。 输入
发布评论