Advertisement
shamiul93

LightOJ 1007 - Mathematically Hard

Feb 25th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. /**
  2. @author - Rumman BUET CSE'15
  3.  
  4. Problem - 1007 - Mathematically Hard
  5. Link - http://www.lightoj.com/volume_showproblem.php?problem=1007
  6.  
  7. Concept -
  8. Euler Totient Function :
  9.  
  10.     You are given a number n . How many relative primes has this number those are smaller
  11.     than n ?
  12.     Ans: Euler has given this formula for that.
  13.  
  14.         Phi = n * (1 - 1/p1) * (1- 1/p2) * (1- 1/p3)*....*( 1 - 1/Pn)
  15.     where , p1 , p1 , p3, .... - are the prime divisors of n
  16.  
  17.     ** Now all you have to do is optimization.
  18.  
  19.     How is that?
  20.     Ans:
  21.         *Memory: I can take 5*10^6 sized array of even Unsigned Long Long if I declare it globally
  22.         or by malloc() ;
  23.  
  24.         *Time: At first , use sieve , then get all phi of the numbers. And then squares , and sum of
  25.         previous ones ( Similar to Fibonacci series) .
  26.  
  27.         * ans = phi[i]-phi[i-1] ;
  28.  
  29.         So, this is the rule.
  30. */
  31.  
  32.  
  33. #include<bits/stdc++.h>
  34. using namespace std ;
  35. #define ll unsigned long long /// ULL *** BTW , For ULL format specifier is %llu.. ***
  36. #define fo freopen("out.txt","w",stdout)
  37. #define fi freopen("in.txt","r",stdin)
  38.  
  39. #define sz 5000007
  40.  
  41. bool prime[sz] = {true} ;
  42. vector<ll> v ;
  43.  
  44. void seive()
  45. {
  46. //    cout << "lI" << endl ;
  47.  
  48.     for(ll i = 0 ; i < sz ; i++)
  49.     {
  50.         prime[i] = true ;
  51.     }
  52.     prime[0] = false ;
  53.     prime[1] = false ;
  54.     prime[2] = true ;
  55.  
  56.     for(ll i = 4 ; i < sz ; i+=2)
  57.     {
  58.         prime[i] = false ;
  59.     }
  60.  
  61.     for(ll i = 3 ; 2*i<= sz ; i+=2)
  62.     {
  63.         if(prime[i]==true)
  64.         {
  65.             for(ll j = 2*i ; j < sz ; j+=i)
  66.             {
  67.                 prime[j] = false ;
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. void primegenerator()
  75. {
  76. //     cout << "hi";
  77.     for(ll i = 0  ; i< sz ; i++)
  78.     {
  79.         if(prime[i]==true)
  80.         {
  81.             v.push_back(i);
  82.         }
  83.     }
  84. //cout << "jikk" ;
  85. }
  86.  
  87. ll phi[sz] ;
  88.  
  89. void Phigenerator()
  90. {
  91.     for(ll i = 0 ; i < sz ; i++)
  92.     {
  93.         phi[i] = i ;
  94.     }
  95.  
  96.     for(ll i = 0 ; i < v.size() ; i++)
  97.     {
  98.         for(ll j = v[i] ; j< sz ; j+=v[i])
  99.         {
  100. //            cout << phi[j] << " " << v[i]-1 << " " << v[i] << endl ;
  101.             phi[j] = ( phi[j] * (v[i]-1) )  / v[i] ;
  102.  
  103. //            cout << phi[j] << " " << v[i]-1 << " " << v[i] << endl ;
  104.  
  105.         }
  106.     }
  107.  
  108.     for(ll i = 1 ; i < sz ; i++)
  109.     {
  110.         phi[i] = phi[i] * phi[i] ;
  111.         phi[i] = phi[i] + phi[i-1] ;
  112.     }
  113. }
  114.  
  115. int main()
  116. {
  117. //    fi;
  118. //    fo ;
  119.     seive() ;
  120.     primegenerator();
  121.     Phigenerator();
  122.  
  123. //    for(ll i = 0 ; i< 100 ; i++ )
  124. //    {
  125. //        cout << i << " - "  << phi[i] << endl ;
  126. //    }
  127.  
  128.  
  129.     ll T, t=0 ;
  130.     scanf("%llu",&T);
  131.  
  132.     while(T--)
  133.     {
  134.         t++ ;
  135.         ll a, b ;
  136.         ll ans ;
  137.  
  138.         scanf("%llu %llu",&a, &b);
  139.  
  140.         if(a > b)
  141.         {
  142.             swap(a,b);
  143.         }
  144.  
  145.         ans = phi[b] - phi[a-1] ;
  146.  
  147.         printf("Case %llu: %llu\n",t,ans);
  148.     }
  149.  
  150.     return 0 ;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement