Advertisement
Guest User

Untitled

a guest
May 24th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Solution {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. seivePrimes();
  8. for (int i = 0; i < n; i++) {
  9. long a = sc.nextLong();
  10. long sqrt = (int) Math.sqrt(a);
  11. if (a >= 4 && sqrt * sqrt == a) {
  12. if (isPrime((int) sqrt)) {
  13. System.out.println("YES");
  14. } else {
  15. System.out.println("NO");
  16. }
  17. } else {
  18. System.out.println("NO");
  19. }
  20. }
  21. sc.close();
  22. }
  23.  
  24. static int maxlim = 1000000;
  25.  
  26. private static void seivePrimes() {
  27. for (int i = 3; i * i <= maxlim; i += 2) {
  28. if (!isNotPrime[(i - 3) / 2]) {
  29. for (int j = i; i * j <= maxlim; j += 2) {
  30. isNotPrime[(i * j - 3) / 2] = true;
  31. }
  32. }
  33. }
  34. }
  35.  
  36. static boolean[] isNotPrime = new boolean[(maxlim - 3) / 2 + 1];
  37.  
  38. private static boolean isPrime(int t) {
  39. if (t == 2)
  40. return true;
  41. if (t % 2 == 0)
  42. return false;
  43. else
  44. return !isNotPrime[(t - 3) / 2];
  45. }
  46. }
  47.  
  48. #include <iostream>
  49. #include <set>
  50.  
  51. using namespace std;
  52.  
  53. const int sqrt_lim = 1000001;
  54.  
  55. set<long long> prime_squares()
  56. {
  57. static bool arr[sqrt_lim];
  58.  
  59. for (int i = 2; i*i < sqrt_lim; i++)
  60. {
  61. if (!arr[i])
  62. {
  63. for (int j = i*i; j < sqrt_lim; j += i)
  64. {
  65. arr[j] = true;
  66. }
  67. }
  68. }
  69.  
  70. set<long long> res;
  71. for (int i = 2; i < sqrt_lim; i++)
  72. {
  73. if (!arr[i])
  74. res.insert((long long)i * i);
  75. }
  76. return res;
  77. }
  78.  
  79. int main()
  80. {
  81. ios_base::sync_with_stdio(false); cin.tie(NULL);
  82.  
  83. set<long long> sq(prime_squares());
  84.  
  85. int n; cin >> n;
  86. for (int i = 0; i < n; i++)
  87. {
  88. long long x; cin >> x;
  89.  
  90. if (sq.find(x) != sq.end())
  91. {
  92. cout << "YESn";
  93. }
  94. else
  95. {
  96. cout << "NOn";
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement