We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>#define ll long long#define ull unsigned long long#define mod 1000000007usingnamespacestd;ullbinpow(ullx,ully)/* (x^y)%p in O(log y) */{ullres=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=(x*x);}returnres;}llbinpowmod(llx,lly,llp)/* (x^y)%p in O(log y) */{ullres=1;x=x%p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}returnres;}llmod_inverse(lln,llp)/* Returns n^(-1) mod p */{returnbinpowmod(n,p-2,p);}intgcd(intx,inty){if(y==0)returnx;returngcd(y,x%y);}boolcomp(pair<int,int>x,pair<int,int>y){returnx.first<y.first;}boolcomp_pairs_by_s(pair<ll,ll>&x,pair<ll,ll>&y){returnx.second<y.second;}boolisPowerOfTwo(llx){/* First x in the below expression is for the case when x is 0 */returnx&&(!(x&(x-1)));}classcmp//comparator for priority_queue {//declaration: priority_queue<int,vector<int>,cmp>public:booloperator()(pair<int,int>A,pair<int,int>B){if(abs(A.first-A.second)==abs(B.first-B.second))returnA.first>B.first;returnabs(A.first-A.second)<abs(B.first-B.second);}};intprime[10005]={0};voidsieve(void){inti,j;for(i=0;i<10005;i++)prime[i]=1;prime[0]=0,prime[1]=0;for(i=2;i<=sqrt(10005);i++){if(prime[i]){for(j=i*i;j<10005;j+=i){prime[j]=0;}}}}voidswap(ll&x,ll&y){inttemp=x;x=y;y=temp;}unsignedintonesComplement(unsignedintn){// Find number of bits in the given integer intnumber_of_bits=floor(log2(n))+1;// XOR the given integer with poe(2, // number_of_bits-1 and print the result return((1<<number_of_bits)-1)^n;}llcnt(ulllimit,ullnum=1,ulllen=1){if(limit<=9)returnlimit+1;if(num>limit)return-1;if(num>=binpow(10,len))return0;if(num<binpow(10,len-1))return-1;if(len==1){llans=5;for(lli=5;i<=9;++i)ans+=1+max(0LL,cnt(limit,i*(len+1),len+1));returnans;}llans=1;lli=0;while(1){llc=cnt(limit,num*(len+i),len+i);if(c==-1)break;ans+=c;i++;}returnans;}voidsolve(){ulll,r;cin>>l>>r;cout<<cnt(r)-(l==0?0:cnt(l-1));cout<<'\n';}intmain(){ios_base::sync_with_stdio(false);cin.tie(NULL);intt;cin>>t;while(t--)solve();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Strange numbers
You are viewing a single comment's thread. Return to all comments →
My recursive solution to this problem---