Codeforces Round #461 C. Cave Painting

C. Cave Painting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Input

The only line contains two integers nk (1 ≤ n, k ≤ 1018).

Output

Print "Yes", if all the remainders are distinct, and "No" otherwise.

You can print each letter in arbitrary case (lower or upper).

Examples
input
4 4
output
No
input
5 3
output
Yes
Note

In the first sample remainders modulo 1 and 4 coincide.


At the beginning of this question, I didn't understand the meaning of the title. The question said that it is given to an n and k, and asked whether the mod of n% (1........k) is not repeated. If it is not repeated, it is Yes, otherwise No.

My method is to use map, and save the duplicates to see if they will appear again.

#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;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325524702&siteId=291194637