Niloy007

Finding the Kth Prime || spoj

May 13th, 2020
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #define max 90000000
  3. #define maxN 1000000
  4. #define mod 10000000 + 5
  5. #define pb push_back
  6. #define pairs pair<int, int>
  7. #define vi vector<int>
  8. #define vb vector<bool>
  9. #define vii vector<pairs>
  10. #define lb lower_bound
  11. #define ub upper_bound
  12. #define lli long long int
  13. #define endl '\n'
  14. using namespace std;
  15.  
  16. bool isPrime[max];
  17. int Prime[maxN];
  18.  
  19. void sieve() {
  20.     for (int i = 1; i <= max; i++) {
  21.         isPrime[i] = 1;
  22.     }
  23.     isPrime[0] = 0;
  24.     for (int i = 2; i * i <= max; i++) {
  25.         if(isPrime[i]) {
  26.             for (int j = i * i; j <= max; j += i) {
  27.                 isPrime[j] = 0;
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. int main() {
  34. #ifdef Niloy
  35.     freopen("input.txt", "r", stdin);
  36.     freopen("output.txt", "w", stdout);
  37. #endif
  38.  
  39.     sieve();
  40.     ios_base::sync_with_stdio(false);
  41.     cin.tie(NULL);
  42.  
  43.     for (int i = 1, j = 0; i <= max; i++) {
  44.         if(isPrime[i]) {
  45.             Prime[j] = i;
  46.             j++;
  47.         }
  48.     }
  49.  
  50.     int t, n;
  51.     cin >> t;
  52.     while(t--) {
  53.         cin >> n;
  54.         cout << Prime[n] << endl;
  55.     }
  56.  
  57.     return 0;
  58. }
RAW Paste Data