Advertisement
Imran_Mohammed

Number of divisor

Feb 26th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  5. #define endl '\n'
  6.  
  7. //Prime Generation
  8. const long long mx = 1e6+123;
  9. bool is_prime[mx];
  10. vector<long long> prime;
  11.  
  12. void primegen ( long long n )
  13. {
  14.     for ( int i = 3; i <= n; i += 2 ) is_prime[i] = 1;
  15.  
  16.     int sq = sqrt ( n );
  17.     for ( int i = 3; i <= sq; i+= 2 ) {
  18.         if ( is_prime[i] == 1 ) {
  19.             for ( int j = (i*i); j <= n; j += ( i + i ) ) {
  20.                 is_prime[j] = 0;
  21.             }
  22.         }
  23.     }
  24.  
  25.     is_prime[2]=1;
  26.     prime.push_back(2);
  27.     for ( int i = 3; i <= n; i += 2 ) {
  28.         if ( is_prime[i] == 1 ) prime.push_back ( i );
  29.     }
  30. }
  31.  
  32. //prime Factorization
  33.  
  34.     int NOD(long long n){
  35.         int ret=1;
  36.  
  37.     for( auto p:prime){
  38.         if( p*p > n)break;
  39.         if(n%p == 0){
  40.  
  41.                 int cnt =1;
  42.  
  43.             while(n%p == 0){
  44.  
  45.                 n = n/p;
  46.                 cnt++;
  47.             }
  48.  
  49.             ret *= cnt;
  50.         }
  51.     }
  52.  
  53.     if(n > 1)ret *= 2;
  54.     return ret;
  55. }
  56.  
  57. //main function
  58. int main()
  59. {
  60.     optimize()
  61.  
  62.     primegen(100);
  63.  
  64.     int n;
  65.     cin >> n;
  66.  
  67.     cout << NOD(n) << endl;//60 = 12
  68.  
  69.  
  70.     return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement