Advertisement
shamiul93

Lightoj 1028 - Trailing Zeroes (I)

Feb 23rd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. /**
  2. @author: Rumman BUET CSE'15
  3. Problem: 1028 - Trailing Zeroes (I)
  4. Problem Link: http://www.lightoj.com/volume_showproblem.php?problem=1028
  5.  
  6. Concept:
  7.  
  8. This problem is a perfect example for prime factorization and
  9. counting the number of all factors.
  10.  
  11. 1. At first we use sieve from 2 to 10^6 (square root of 10^12)
  12. 2. In vector v we put all the primes from this sieve
  13. 3. Now we take the input of the number. We divide and modulo this number by
  14.    all the primes smaller than it from vector v
  15. 4. Thus we can count how many times a prime factor is multiplied inside it.
  16. 5. We know that all integers can be prime factorized. Now this happened here.
  17. 6. Now the rest is HSC Level Higher Math problem.
  18.    -> An integer has prime factors 2,3,5 respectively 4,3,1 times.
  19.    How many factors does it have?
  20.  
  21.    Ans: Every number can be taken as many times as it appears. i.e. respectively
  22.    2 or 3 or 5 times. And there is another way for a which is - "Taking zero times."
  23.    so , ans = (2+1)*(3+1)*(5+1) - 1 .
  24.    Why (-1) ? because , there is one way which means - "Taking no prime factors".
  25.    That means 0 . - That would make no sense.
  26.  
  27.    That's all.
  28.  
  29. */
  30.  
  31. #include <bits/stdc++.h>
  32. #define ll                                      long long
  33. #define fi                                      freopen("in.txt", "r", stdin)
  34. #define fo                                      freopen("out.txt", "w", stdout)
  35.  
  36. using namespace std;
  37.  
  38. #define sz 1000010
  39.  
  40. vector<ll> v ;
  41.  
  42. int main()
  43. {
  44. //    fi;
  45. //    fo;
  46.  
  47.     bool arr[1000010] = {} ;
  48. //    cout << "hi" << endl ;
  49.     memset(arr, true, sizeof(arr));
  50.     arr[0] = false ;
  51.     arr[1] = false ;
  52.  
  53.  
  54.     v.push_back(2);
  55.  
  56. // Here it's optimized again. We don't need to mark even numbers as false here. So, we saved a loop 10^6/2 times iterating. :3
  57.  
  58.     for(ll i = 3 ; i < sz ; i+=2)
  59.     {
  60.         if(arr[i] == true )
  61.         {
  62.             v.push_back(i);
  63.             for(ll j = 2*i ; j < sz ; j+= i)
  64.             {
  65.                 arr[j] = false ;
  66.             }
  67.         }
  68.     }
  69.  
  70.     ll T, t = 0 ;
  71.  
  72.     scanf("%lld",&T);
  73.  
  74.     while(T--)
  75.     {
  76.         t++ ;
  77.         ll n ;
  78.         scanf("%lld",&n);
  79.  
  80.         ll a = n ;
  81.         ll ans = 1 ;
  82.  
  83.         /// v[i]*v[i] <= a - At first I wrote n in stead of a . Got WA several times.
  84.         for(ll i= 0 ; i< v.size(), a > 1, v[i]*v[i] <= a ; i++)
  85.         {
  86.  
  87.             ll oc = 0 ; /// occurrence number of a certain factor
  88.  
  89.             ll r = 0 ;
  90.             while(r == 0 && a > 1)
  91.             {
  92.                 r = a % v[i] ;
  93.  
  94.                 if(r==0)
  95.                 {
  96.                     oc++ ;
  97.                     a = a / v[i] ;
  98.                 }
  99.  
  100.             }
  101.             if(oc > 0)
  102.             {
  103.                 ans = ans * (oc + 1) ;
  104.             }
  105.         }
  106.  
  107.  
  108.  
  109.  
  110.         if(a > 1)
  111.         {
  112.             ans *= (1+1) ;
  113.         }
  114.  
  115.         /**
  116.          * Question may arise why  ans *= (1+1) ; in the next line?
  117.          Ans: If a quotient is nonzero after dividing it with all the primes,
  118.          and yet it's non zero except 1 , then it's a PRIME Itself. So , take it .
  119.  
  120.          * Only one prime factor of a number can be greater than sqrt of n.
  121.          a*b*c = n (say)
  122.          say , a > sqrt(n) , b> sqrt(n) , c< sqrt(n)
  123.          a*b > n ; c is not even multiplied yet.
  124.          SO, you see...
  125.         */
  126.  
  127.  
  128.         printf("Case %lld: %lld\n",t, ans - 1 ) ;
  129.  
  130.     }
  131.  
  132.     return 0 ;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement