T-Primes

We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t 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 integers xi (1 ≤ xi ≤ 1012).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.

Output
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>


#define fl(i, n) for(int i = 0; i < n; i++)

#define ll   long long
#define nl   endl
#define pb   push_back
#define mp   make_pair
#define PII  pair<int,int>

#define EPS  1e-9
#define INF  1e9


using namespace std;

set<long long> a;
ll b[1000000+1];
bool primes[1000000+1];
int k = 0;

void order(){
memset(primes,true,sizeof(primes));
primes[0] = primes[1] = 0;
for(ll i = 2 ; i <= sqrt(1e12)+1; i++){
    if(primes[i]){
        for(int j = i*2; j <= sqrt(1e12)+1; j+=i){
            primes[j] = 0;
        }
    }
}
for(ll i = 2; i <= sqrt(1e12)+1; i++){
    if(primes[i]){
        a.insert(i*i);
    }
}
}


int main()
{
    int n;
    cin >> n;
    order();


    for(int i = 0; i < n; i++){
        cin >> b[i];
        if(a.find(b[i]) != a.end()){
            cout << "YES" << nl;
        }
        else cout << "NO" << nl;
    }


    return 0;
}
Last edited on
Hint 1: Sieve of Eratosthenes https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Hint 2: T-Primes are squares of prime numbers.
(Its positive divisors are 1, the number itself and the square root of the number).
Topic archived. No new replies allowed.