题意:给出两个数n和k。问n%i (1<=i<=k) 的结果是否都互不相等,如果是则输出Yes,否则输出No。
第一个例子:
4%1==0
4%2==0
4%3==1
4%4==0
故输出No
第二个例子:
5%1==0
5%2==1
5%3==2
故输出Yes
解题思路:
从i等于1时入手。任何n%1都等于0
i==2时 n%2结果要么为0要么为1 0已经被占用 所以n%2只能等于1
i==3 n%3==2
i==4 n%4==3
: :
: :
: :
i==k n%k==k-1
所有判断当前n%i的结果是否等于i-1,若不等则直接输出no
那么n与k有什么关系呢?
通过k我们能否求出最小的n是多少?
n==lcm(1...k)-1
即是(1到k之间所有的数的最小公倍数)-1
因为lcm+1对所有i (1<=i<=k)都能整除 即 (lcm+1)%i==0
那么(lcm-1)%i==i-1
如:lcm(1...4)==12
12%1==0(可以看作余数为1) 11%1==0(1-1==0)
12%2==0(可以看作余数为2) 11%2==1(2-1==1)
12%3==0(可以看作余数为3) 11%3==2(3-1==2)
12%4==0(可以看作余数为4) 11%4==3(4-1==3)
可以看作余数为x的意思是i是x的整数倍 n能被i整除 结果为0
所以对于题目数据中的n,k最大不会超过50
#include<stdio.h>
int main()
{
long long k,n;
while(~scanf("%I64d%I64d",&n,&k))
{
int flag=0;
for(int i=1;i<=k;i++)
{
if(n%i!=i-1)
{
flag=1;
break;
}
}
if(flag) printf("No\n");
else printf("Yes\n");
}
return 0;
}