Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int limit = 1e4 + 4;
  6. vector<int> prime(limit);
  7.  
  8. void preprocess(){
  9.     prime[0] = 0;
  10.     prime[1] = 1;
  11.     for(int i=2;i<=limit;i++) prime[i] = 0;
  12.     for(int i=2;i*i<=(limit);i++){
  13.         if(prime[i]==0){
  14.             prime[i]=i;
  15.             for(int j=i*i;j<=limit;j+=i){
  16.                 if (prime[j] == 0) prime[j] = i;
  17.             }
  18.         }
  19.     }
  20. }
  21.  
  22. void process(){
  23.     int n;
  24.     cin >> n;
  25.     for(int i=1;i<=n;i++){
  26.          cout << prime[i] << " ";
  27.     }
  28.     cout << endl;
  29. }
  30.  
  31. int main(){
  32.     preprocess();
  33.     int T;
  34.     cin >> T;
  35.     while (T--)
  36.     {
  37.         process();
  38.     }
  39.    
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement