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的最后一个数字。

输入

输入数据有多组,每组数据占一行,每行为a和b的值(0

输出

对每组输入数据,输出a^b的最后一位数字,每组数据占一行。

样例输入

2 2

3 4

样例输出

4

1

思路/方法:如果你模拟a^b次肯定会超时,而且数字还会超出Int范围

题目说只要最后一个数字,回想一下小学时学的乘法过程,貌似乘数最后一位和前面的无关

我们大胆的尝试一下用a的个位代替a,然后我们发现循环b次还是会超时,这是我们要想办法减少循环的次数,试一下是不是有周期

规律

这时我们来列举一下2的n次方的个位:2 4 8 6 2 4 8 6

我们发现周期为4,我们在试试1-9的n次方,发现周期都是4,所以,我们可以用b%4代替b,需要注意的是,当b%4==0时,我们需

要给他加上4,不然就不循环了。

代码:

#include

int main()

{

int a, b, i, t;

while(scanf("%d %d", &a, &b) != EOF)

{

b = b % 4;

if(b == 0)

b = 4;

a = a % 10;

t = 1;