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)的mod是否不重复,不重复为Yes,否则No
我的方法是用map过的,存了一下重复的,看看还会不会再出现
#include <iostream>
#include <map>
using namespace std;
map<long long,int>mymap;
int main() {
long long x,y;
cin>>x>>y;
int i;
for(i=1;i<=y;i++){
if(mymap[x%i]!=0)
{
cout<<"No"<<endl;
break;
}
else
mymap[x%i]++;
}
if(i>y)
cout<<"Yes"<<endl;
return 0;
}
后来看别人的代码,发现有写的更加好的代码,所以分享一下
如果是Yes 得到的余数按除数从1~k排列下来是 0 1 2 3 4 5 ........ k-1
所以n+1除以1~k的余数应该是0 0 0 0 0 ....... 0
所以遍历一个k就可以了,你会觉得会TLE,其实Yes的都是小数,大数都是No,所以O(n)的复杂度不会TLE
#include <iostream>
using namespace std;
int main() {
long long i,x,y;
cin>>x>>y;
x++;
for( i=1;i<=y;i++)
if(x%i)
{
printf("No\n");
break;
}
if(i>y)
printf("Yes\n");
return 0;
}