Oibek

Linear Sieve

Mar 26th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. #define pb push_back
  6.  
  7. int main() {
  8.     const int n = 1e5;
  9.     int lp[n+1];
  10.     vector<int> pr;
  11.  
  12.     for (int i = 2; i<=n; i++)
  13.     {
  14.         if (lp[i]==0)
  15.         {
  16.            lp[i] = i;
  17.            pr.pb(i);
  18.         }
  19.  
  20.         for (int j = 0; j<pr.size() && pr[j]<=lp[i]; i*pr[j]<=n; j++)
  21.             lp[ i*pr[j] ] = pr[j];
  22.     }
  23.  
  24.     // factorzation
  25.     int a; cin >> a;
  26.     while (a!=1)
  27.     {
  28.         cout << lp[a] << ' ';
  29.         a/=lp[a];
  30.     }
  31. }
Add Comment
Please, Sign In to add comment