T-primes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integert Т-prime, ift has exactly three distinct positive divisors.
You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
Input
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integersxi (1 ≤ xi ≤ 1012).
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use thecin, cout streams or the%I64d specifier.
Output
Print n lines: the i-th line should contain "YES" (without the quotes), if numberxi is Т-prime, and "NO" (without the quotes), if it isn't.
Examples
Input
3
4 5 6
Output
YES
NO
NO
Note
The given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
若一个数只有三个约数,则这个数必为完全平方数,且其平方根必为素数,三个月数分别为1,本身,还有它的平方根。
故解题思路为:完全平方数判断+素数判断(素数筛),代码如下:
#include<iostream>
#include<cmath>
#include<cstring>
# define m 1000005
using namespace std;
bool pri[m];
void getpri()
{
int i,j;
pri[0]=pri[1]=0;
pri[2]=1;
for(i=3;i<=m;++i) pri[i]=i%2==0?0:1;
int t=sqrt(m);
for(i=3;i<=t;++i)
{
if(pri[i])
{
for(int j=i*i;j<=m;j+=2*i) pri[j]=0;
}
}
}
int main()
{
long long n,a,t;
getpri();
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>a;
t=sqrt(a);
if(t*t==a&&a!=1&&pri[t]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
本文介绍了一种特殊类型的整数——T-Prime数,并提供了一个高效的算法来判断一个数是否为T-Prime数。T-Prime数定义为恰好拥有三个不同正因数的正整数。文章还给出了具体的代码实现,利用完全平方数判断与素数筛法来完成这一任务。
706

被折叠的 条评论
为什么被折叠?



