Maruf_Hasan

PrimeFactorization using Sieve

Nov 4th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. //In the Name of Allah Most Gracious, Most Merciful//
  2. /*If you want something you've never had, you have to do something you never did.*/
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. #define pb push_back
  9. #define ll long long
  10. #define pii pair<ll,ll>
  11. #define pll pair<ll,ll>
  12. #define M 10000007
  13. #define INF 1e9
  14. #define INFL 1e18
  15. #define PI acos(-1)
  16. #define mp make_pair
  17. #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  18. //const ll fx[]= {+1,-1,+0,+0};
  19. //const ll fy[]= {+0,+0,+1,-1};
  20. //const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  21. //const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  22. //const ll fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  23. //const ll fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  24. ll SPF[M+10];
  25. bool mark[M+10];
  26.  
  27. void sieve_SPF()
  28. {
  29. for(ll i=2;i<=M;i+=2)
  30. {
  31. SPF[i]=2;
  32.  
  33. }
  34.  
  35. for(ll i=3;i<M;i+=2)
  36. {
  37. if(mark[i]==false)
  38. {
  39. SPF[i]=i;
  40. for(ll j=i;(j*i)<M;j+=2)
  41. {
  42.  
  43. if(mark[i*j]==false)
  44. {
  45. mark[i*j]=true;
  46. SPF[i*j]=i;
  47. }
  48. }
  49. }
  50. }
  51. }
  52.  
  53. vector<int> get_prime_div(int x)
  54. {
  55. vector<int>ret;
  56. while(x!=1)
  57. {
  58. ret.pb(SPF[x]);
  59. x=x/SPF[x];
  60.  
  61. }
  62. return ret;
  63. }
  64. int main()
  65. {
  66. fast_in_out;
  67. // #ifndef ONLINE_JUDGE
  68. // freopen("input.txt","r",stdin);
  69. // freopen("output.txt","w",stdout);
  70. // #endif
  71. //cout<<"AS"<<endl;
  72. sieve_SPF();
  73. // cout<<"asd"<<endl;
  74. for(ll i=2;i<=100;i++)
  75. {
  76. vector<int>v;
  77. v=get_prime_div(i);
  78. cout<<"NUM "<<i<<": ";
  79. for(int i=0;i<v.size();i++)
  80. {
  81. cout<<v[i]<<" ";
  82. }
  83. cout<<endl;
  84. }
  85.  
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment