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≤1e9.
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 |
解释:题目的意思是给出一个数q,问你是否存在p和k,满足q=p^k,其中p为素数,k>=1,如果满足,输出yes,否则输出no
q的取值为1e9,所以我们要先用试除法判断q是否为素数,接着判断2-sqrt(n)之间是否有满足条件的素数,即从2到sqrt(n)用一次筛素数
方法一 :线性筛法
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
bool vis[N];
int a[N];
int cnt;
bool isprime(int n)//判断是否为素数
{
if (n == 1)
return 0;
if (n == 2)
return 1;
for (int i = 2; i <= N; i++)
{
if (n % i == 0)
return 0;
}
return 1;
}
bool find(int n)//线性筛法
{
for (int i = 2; i <= N; i++)
{
if (!vis[i])
a[cnt++] = i;
for (int j = 0; i * a[j] < N && j < cnt; j++)//用i*a[j]保证vis的下标不会越界
{
vis[i * a[j]] = 1;
if (i % a[j] == 0)
break;
}
}
for (int i = 0; i < cnt; i++)
{
for (int j = a[i]; j <= n; j *= a[i])
{
if (j == n)
return 1;
}
}
return 0;
}
signed main()
{
int n;
scanf("%d", &n);
if (isprime(n))
printf("yes");
else
{
if (find(n))
printf("yes");
else
printf("no");
}
return 0;
}
方法二 埃式筛法
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
#define int long long
bool vis[N];
int a[N];
int cnt;
bool isprime(int n)
{
if (n == 1)
return 0;
if (n == 2)
return 1;
for (int i = 2; i <= n / i; i++)
{
if (n % i == 0)
return 0;
}
return 1;
}
bool find(int n)
{
for (int i = 2; i <= N; i++)
{
if (!vis[i])
{
if (n % i == 0)
{
while (n % i == 0)
{
n /= i;
}
if (n == 1)
return 1;
else
return 0;
}
for (int j = i * 2; j < N; j += i)
{
vis[j] = 1;
}
}
}
return 0;
}
signed main()
{
int n;
cin >> n;
if (n <= 1)//对于1我们要特判,否则1会产生find(1),继而run-time error
printf("no");
else if (isprime(n))
printf("yes");
else
{
if (find(n))
printf("yes");
else
printf("no");
}
return 0;
}