Title Link: Http://codeforces.com/problemset/problem/55/D
Test instructions: If a number can be divisible by each of her non-0 bits, then this number is called beautiful numbers.
Given L and R, the total number of beautiful numbers in the Q interval [l,r].
Ideas:
Test instructions is the number that can be divisible by each of its non-0-bit least common multiple.
Because every bit is 1,2,3,4,5,6,7,8,9. That is, the worst case of this number can be divisible by 1*2*3*4*5*6*7*8*9 = 2520.
Set this number to X, set x = 2520*t+y. The LCM is the least common multiple of the number of occurrences of each non-0-bit X.
Then you can know 2520% LCM = 0.
So the x LCM = X%2520%LCM. So in the memory of the search, just save the current number of the remainder of the%2520.
So need a DP[CUR][LAST][LCM] size has 19*2520*2520 to hyper-memory, so the recording of the LCM to disperse a bit.
1#include <bits/stdc++.h>2 using namespacestd;3 Long LongL, R;4 intnum[ A];5 inthash[2550];6 Long Longdp[ A][2530][ -];7 voidInit ()8 {9Memset (DP,-1,sizeof(DP));Ten intCNT =0; Onehash[0] =0; A for(inti =1; I <=2520; i++) - { - if(2520%i = =0) Hash[i] = + +CNT; the } - } - - intgcdintAintb) + { - if(A < b)returngcd (b, a); + if(b = =0)returnA; A returnGCD (b, a%b); at } - intGETLCM (intAintb) - { - returnA/GCD (A, b) *b; - } - Long LongDfsintCurintLastintLcmintlimit) in { - if(Cur <0) to { + if(last% LCM = =0)return 1; - Else return 0; the } * if(!limit && DP[CUR][LAST][HASH[LCM]]! =-1)returnDP[CUR][LAST][HASH[LCM]]; $ Panax Notoginseng Long LongRET =0; - intup = Limit?num[cur]:9; the for(inti =0; I <= up; i++) + { A inttemp = (last*Ten+i)%2520; the intNOWLCM; + if(I! =0) NOWLCM =GETLCM (LCM, i); - ElseNOWLCM =LCM; $RET + = DFS (cur-1, temp, NOWLCM, limit && i = =Up ); $ } - if(!limit) DP[CUR][LAST][HASH[LCM]] =ret; - returnret; the } - Long LongSlove (Long Longx)Wuyi { the intCNT =0; - while(x) Wu { -num[cnt++] = x%Ten; AboutX/=Ten; $ } - returnDFS (cnt-1,0,1,1); - } - intT; A intMain () + { the init (); -scanf"%d", &T); $ while(t--) the { theCin>>l>>R; theCout<<slove (R)-Slove (L-1) <<Endl; the } - return 0; in}
Codeforces 55D Beautiful Numbers