DuongNhi99

THREEPRIMES (Thi thử DHBB)

Mar 10th, 2022
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e7;
  5.  
  6. // Sàng nguyên tố
  7. vector<bool> isPrime;
  8. void sieve() {
  9.     isPrime.assign(N + 1, true);
  10.     isPrime[0] = false;
  11.     isPrime[1] = false;
  12.    
  13.     for (int i = 2; i * i <= N; ++i) {
  14.         if (isPrime[i] == true) {
  15.             for (int j = i * i; j <= N; j += i)
  16.                 isPrime[j] = false;
  17.         }
  18.     }
  19. }
  20.  
  21. // Giải bài toán - O(n^(1,367))
  22. int query(int n) {
  23.     for (int p = 2; p <= N; ++p)
  24.         if (isPrime[p]) {
  25.             if (isPrime[p * 2 - 1]) {
  26.                 if (--n == 0) {
  27.                     cout << p << ' ' << p << ' ' << p * 2 - 1;
  28.                     return 0;
  29.                 }
  30.             }
  31.  
  32.             if (isPrime[p * 2 + 1]) {
  33.                 if (--n == 0) {
  34.                     cout << p << ' ' << p << ' ' << p * 2 + 1;
  35.                     return 0;
  36.                 }
  37.             }
  38.         }
  39. }
  40.  
  41. int main() {
  42.     ios_base::sync_with_stdio(0);
  43.     cin.tie(NULL);
  44.  
  45.     sieve();
  46.     int n; cin >> n;
  47.     query(n);    
  48.    
  49.     return 0;
  50. }
  51.  
  52. /*
  53. #include <bits/stdc++.h>
  54. using namespace std;
  55.  
  56. const int N = 1e7;
  57.  
  58. // Sàng nguyên tố tuyến tính - O(N)
  59. vector<int> prime;
  60. vector<int> lpf;
  61. void sieve() {
  62.     prime.assign(1, 2);
  63.     lpf.assign(N + 1, 2);
  64.  
  65.     for (int x = 3; x <= N; x += 2) {
  66.         if (lpf[x] == 2)
  67.             prime.push_back(lpf[x] = x);
  68.        
  69.         for (int i = 0; i < prime.size() && prime[i] <= lpf[x] && prime[i] * x <= N; ++i)
  70.             lpf[prime[i] * x] = prime[i];
  71.     }
  72. }
  73.  
  74. // Kiểm tra nguyên tố - O(1)
  75. bool isPrime(int x) {
  76.     return (x > 1) && (lpf[x] == x);
  77. }
  78.  
  79. // Giải bài toán - O(n^(1,367))
  80. int query(int n) {
  81.     for (int p : prime) {
  82.         if (isPrime(p * 2 - 1)) {
  83.             if (--n == 0) {
  84.                 cout << p << ' ' << p << ' ' << p * 2 - 1;
  85.                 return 0;
  86.             }
  87.         }
  88.  
  89.         if (isPrime(p * 2 + 1)) {
  90.             if (--n == 0) {
  91.                 cout << p << ' ' << p << ' ' << p * 2 + 1;
  92.                 return 0;
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. int main() {
  99.     ios_base::sync_with_stdio(0);
  100.     cin.tie(0);
  101.  
  102.     sieve();
  103.     int n; cin >> n;
  104.     query(n);    
  105.    
  106.     return 0;
  107. }
  108. */
Advertisement
Add Comment
Please, Sign In to add comment