Codeforces 230B

来源:互联网 发布:js包装函数是什么 编辑:程序博客网 时间:2024/04/30 18:58
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
34 5 6
Output
YESNONO

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;
}
0 0
原创粉丝点击