Imp is watching a documentary about cave painting.
Some numbers, carved in chaotic order, immediately attracted his attention. Imp rapidly proposed a guess that they are the remainders of division of a number n by all integers i from 1 to k. Unfortunately, there are too many integers to analyze for Imp.
Imp wants you to check whether all these remainders are distinct. Formally, he wants to check, if all , 1 ≤ i ≤ k, are distinct, i. e. there is no such pair (i, j) that:
- 1 ≤ i < j ≤ k,
- , where is the remainder of division x by y.
The only line contains two integers n, k (1 ≤ n, k ≤ 1018).
Print "Yes", if all the remainders are distinct, and "No" otherwise.
You can print each letter in arbitrary case (lower or upper).
4 4
No
5 3
Yes
In the first sample remainders modulo 1 and 4 coincide.
题意:
给你一个n,k,让你判断n对1到k的所有数字取余结果是否有相同的,若有,输出Yes
分析:
一开始看到这个题,想的就是,暴力模拟,然而卡在了数据范围太大,按照我过去的经验,我可能会傻乎乎的(好吧,其实我不想说自己傻....)去想办法解决开不了这么大数组的问题,然而此时,我应该想的是换一种方法来做,正面走不通走反面嘛,分析一下要是保证每一个余数都不同需要满足什么样的条件呢?(不知道我下一次碰到类似的情况能不能这样来思考.....)
我们可以注意到,n对i取余的结果有i种情况,即从0到i-1,那么,n%1=0,n%2就只能是1(要保证每一个余数都不同的话)
所以对于每一个i,n%i==i-1,就可以保证余数都不同了。
所以问题就转换为:判断每一个i,n%i是否等于i-1
代码如下(注意数据范围 开long long ):
1 #include<stdio.h> 2 long long n,k; 3 int main() 4 { 5 6 while(~scanf("%I64d %I64d",&n,&k)) 7 { 8 long long i; 9 for(i=1;i<=k;i++) 10 { 11 if(n%i!=i-1) 12 { 13 printf("No "); 14 break; 15 } 16 } 17 if(i==k+1) 18 printf("Yes "); 19 } 20 21 22 23 24 25 return 0; 26 }
所以,要学会反向思考问题,之前想的是判断每一个余数是否相同,在这种复杂度很高无法实现的情况下,就应该想到换条思路,从别的角度来想这个问题,比如,要使结果为yes,那应该满足什么条件呢?
PS:说起来,很多问题的解决往往就在于换个思路,角度去看,不管是做题还是生活都是这样,看来道理都是相通的,就是不知道自己下次遇到类似问题能不能想到换个思路........