Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <cmath>
  2. #include <vector>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. #define pb push_back
  8. #define ll long long
  9. #define fall(a,c) for(auto &a:c)
  10. #define all(k) k.begin(),k.end()
  11. #define endl '\n'
  12.  
  13. const int P = 2e6+5;
  14. vector<ll> phi(P+1);
  15. void PHI(){//get prime factors and calculate phi
  16.     for(ll i=1;i<P;i++)phi[i]=i;
  17.     for(ll i=2;i<P;i++){//O(N)
  18.         if(phi[i]==i){//n is prime
  19.             for(ll j=i;j<P;j+=i){//O(logi)
  20.                 //i is a prime factor of j
  21.                 phi[j]=phi[j]/i*(i-1);
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. ll memo[P];
  28. ll depth(ll n){
  29.     if(n==1) return 0;
  30.     if(memo[n]!=-1) return memo[n];
  31.     return memo[n] = 1+depth(phi[n]);
  32. }
  33. vector<ll> sk[20];
  34.  
  35. #define endl '\n'
  36. int32_t main(){
  37.     ios::sync_with_stdio(0); cin.tie(0);
  38.     PHI();
  39.     memset(memo, -1, sizeof memo);
  40.     int t; cin>>t;
  41.     for(int i=1;i<=1000000;i++){
  42.         if(depth(i)<20)
  43.             sk[depth(i)].pb(i);
  44.     }
  45.  
  46.  
  47.     while(t--){
  48.         int n,m,k;
  49.         cin>>m>>n>>k;
  50.         auto ans = int(upper_bound(all(sk[k]),n)-sk[k].begin()) - int(lower_bound(all(sk[k]),m)-sk[k].begin());
  51.         cout<<ans<<endl;
  52.     }
  53.  
  54.  
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement