首先考虑 的情况,得到的数就是 。所以判断 是否是质数即可。
然后就是 的情况。对于这种情况,设 位数为 ,手玩一下发现得到的数是 。
所以展开可以知道就是 ,第二个数必不等于 ,所以当 时,不合法;当 时, 合法。特判即可。
#include<bits/stdc++.h>
using namespace std;
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
int T,x,k,i;
bool isprime(int x)
{
int i;
if (x==1) return false;
for (i=2;i<=sqrt(x);i++)
{
if (x%i==0) return false;
}
return true;
}
void work()
{
scan>>x>>k;
if (x==1&&k==2){printf("YES\n");return;}
if (k!=1) printf("NO\n");
else
{
if (isprime(x)) printf("YES\n");
else printf("NO\n");
}
}
int main()
{
scan>>T;
while (T--) work();
return 0;
}