Advertisement
fireLUFFY

RangeQuery(3^a 8 5^b)

Oct 2nd, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define inf 2e18
  5. int MOD=1000000007;//998244353;
  6. const int n=1000050;
  7. bool prime[n];    //array to store precalculated primes till 10^6
  8. void cal_primes(){memset(prime,true,sizeof(prime)); for(int i=2;i<=sqrt(n);++i){ if(prime[i]==true){ for(int j=i*i;j<=n;j+=i){prime[j]=false;}}}}
  9.  
  10. bool check(int i)
  11. {
  12.     while(i>0)
  13.     {
  14.         if(i==1)
  15.             return true;
  16.  
  17.         if(i%5==0)
  18.             i/=5;
  19.         else if(i%3==0)
  20.             i/=3;
  21.         else
  22.             return false;
  23.     }
  24. }
  25.  
  26. void solve(int t)
  27. {
  28.     int testcases=t;
  29.     while(t--)
  30.     {
  31.         int l,r,c=0;
  32.         cin>>l>>r;
  33.  
  34.         vector<bool>p(n+1,false);
  35.         vector<int>cnt(n+1,0);
  36.  
  37.         for(int i=0;i<n;i++){
  38.             if(check(i))
  39.                 p[i]=true;
  40.         }
  41.  
  42.         for(int i=1;i<n;i++){
  43.             if(p[i]==true){
  44.                 ++c;
  45.             }
  46.             cnt[i]=c;
  47.         }
  48.  
  49.         int ans;
  50.         if(l==0)
  51.             ans=cnt[r];
  52.         else
  53.             ans=(cnt[r]-cnt[l-1]);
  54.  
  55.         cout<<ans<<endl;
  56.     }
  57. }
  58.  
  59. main()
  60. {
  61.     auto start=chrono::system_clock::now();
  62.     {  
  63.         ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  64.         int t=1;
  65.     //  cin>>t;
  66.         solve(t);
  67.     }
  68.     auto end=chrono::system_clock::now();
  69.     chrono::duration<double> elapsed=end-start;
  70. //    cout<<endl<<"Time taken: "<<elapsed.count()<<" sec";
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement