当前位置:   article > 正文

B. T-primes_b - primes gym - 102267b csdn

b - primes gym - 102267b csdn

B. 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 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 cincout 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.

Sample test(s)
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".


解题说明:此题就是判断一个数是否仅含有3个因子,其实也就是除了1和它自身外,还存在另一个因子。如果找到这个另外的因子就输出yes,否则为no。

首先来分析这个数的情况,由于只有一个不同的因子,那这个数肯定是某个数的平方数,否则不可能只有一个因子。这样问题缩小为在2到n^(1/2)-1之间求因子的个数,如果因子个数不为0,那证明还存在其他因子,不符合题意,我们就证明其中不存在因子即可,问题进一步转换为证明n^(1/2)是个素数。

证明素数的问题很常见,令m=n^(1/2),然后判断m^(1/2)之间有没有因子即可。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. int main()
  8. {
  9. int n,i,j;
  10. long long a,b;
  11. cin>>n;
  12. for(i=1;i<=n;i++)
  13. {
  14. cin>>a;
  15. b=sqrtl(a); //long double sqrtl( long double x );
  16. for(j=2;j*j<=b;j++)
  17. {
  18. if(b%j==0)
  19. {
  20. break;
  21. }
  22. }
  23. if(j*j>b && b*b==a && a>1)
  24. {
  25. cout<<"YES\n";
  26. }
  27. else
  28. {
  29. cout<<"NO\n";
  30. }
  31. }
  32. return 0;
  33. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/75187
推荐阅读
相关标签
  

闽ICP备14008679号