Your older brother is an amateur mathematician with lots of experience. However, his memory is very bad. He recently got interested in linear algebra over finite fields, but he does not remember exactly which finite fields exist. For you, this is an easy question: a finite field of order qq exists if and only if qq is a prime power, that is, q = p^ kq=pk holds for some prime number pp and some integer k \geq 1k≥1. Furthermore, in that case the field is unique (up to isomorphism).
The conversation with your brother went something like this:
Input
The input consists of one integer qq, satisfying 1 \leq q \leq 10^91≤q≤109.
Output
Output “yes” if there exists a finite field of order qq. Otherwise, output “no”.
Sample 1
Inputcopy | Outputcopy |
---|---|
1 | no |
Sample 2
Inputcopy | Outputcopy |
---|---|
37 | yes |
Sample 3
Inputcopy | Outputcopy |
---|---|
65536 | yes |
这个题本来比赛的时候过的还是挺快的,但是今晚帮同学改的时候发现确实很多细节需要注意,所以决定还是写一个博客吧
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;//直接const定义方便易用
//
int prime[N];//改个名 看着方便
int t;
bool isPrime[N];
//
void getPrime()//直接计算N就行
{
/*for(int i=2;i<=N;++i)
isPrime[i] = true;
t=0;
for(int i=2;i<=N;++i)
if(isPrime[i])
{
prime[++t]=i;
for(int j=i;i*j<=N;++j)
isPrime[i*j] = false;
}
*/
int i,j;
for(i=2; i<=N-5; i++) isPrime[i]=true;
isPrime[0]=isPrime[1]=false;
t=0;
for(i=2; i<=N-5; i++){
if(isPrime[i]){
prime[++t]=i;
for(j=2*i; j<=N-5; j+=i){
isPrime[j]=false;
}
}
}
}
//
int judge(long long n)
{
if(n==0||n==1) return 0;
else if(n==2) return 1;//增加两个对特殊情况的判断
for(long long i=2;i<=sqrt(n);i++)
{
if(n%i==0){
return 0;break;
}//这里就需要及时的break掉了
}
return 1;
}
//
int main()
{
long long n;
long long sum;
scanf("%lld",&n);
if(n==1) cout<<"no"<<endl;
else
{
if(judge(n)) cout<<"yes"<<endl;
else
{
getPrime();
//int f=0;
for(long long i=1;i<=t;i++)//注意i的数据范围 longlong
{
sum=1;//sum也是longlong
while(sum<n)//是小于 等于也不行
{
sum *=prime[i];
/*if(sum==n)
{
f=1;
break;
}*///这个位置的if完全没有必要
}
if(sum==n) break;
}
if(sum==n)
cout << "yes" << endl;
else
cout << "no" << endl;
}
}
return 0;
}