Advertisement
dipBRUR

Count Divisor in O(n^1/3)

Jan 31st, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. // Count Divisor in O(n^1/3)
  2. ll countDivisor(ll n) {
  3.     ll ans = 1;
  4.     int len = primeList.size();  // upto 10^6
  5.     for (int i = 0; i < len; i++) {
  6.         ll pNum = primeList[i];
  7.         ll num = pNum*pNum*pNum*1LL;
  8.         if (num > n)    
  9.             break;
  10.         int cnt = 1;
  11.         while (n%pNum==0) {
  12.             n /= pNum;
  13.             cnt++;
  14.         }
  15.         ans *= cnt;
  16.     }
  17.     ll sq = sqrt(n);
  18.     if (isPrime(n)) //Y is prime, F(Y)=2
  19.         ans *= 2;
  20.     else if (sq*sq==n && isPrime(sq))  //Y is square of a prime number, F(Y)=3
  21.         ans *= 3;
  22.     else if (n!=1) //Y is product of two distinct prime numbers, F(Y)=4
  23.         ans *= 4;
  24.     return ans;
  25. }
  26. // 12 = 6 (1, 2, 3, 4, 6, 12)
  27. // 999999999999999989 = 2
  28. // 100000007700000049 = 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement