CF 55D digital dp (a number is a multiple of the corresponding numbers of all its digits), 55ddp
Http://codeforces.com/problemset/problem/55/D
D. Beautiful numberstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Volodya is an odd boy and his taste is strange as well. it seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. we will not argue with this and just count the quantity of beautiful numbers in given ranges.
Input
The first line of the input contains the number of casesT(1 digit ≤ DigitTLimit ≤ limit 10). Each of the nextTLines contains two natural numbersLIAndRI(1 digit ≤ DigitLILimit ≤ limitRICost ≤ cost 9 cost · 1018 ).
Please, do not use % lld specificator to read or write 64-bit integers in C ++. It is preffered to use cin (also you may use % I64d ).
Output
Output shoshould containTNumbers-answers to the queries, one number per line-quantities of beautiful numbers in given intervals (fromLIToRI, Inclusively ).
Sample test (s) input
11 9
Output
9
Input
112 15
Output
2
/** CF 55D digital dp (a number is a multiple of the numbers corresponding to all its digits) Question: Find out how many minority within a given range, it is the thought of solving the multiple multiples of the numbers on each bit of it: (kuangbin) a number can be divisible by all its non-zero digits, and can be divisible by their minimum public multiples, the minimum public multiples of 1 to 9 are 2520. When the number of edas is greater than, we only need to save the minimum public multiples of the previous bits for state transfer. When we reach the boundary, we can find the lcm of all BITs, to determine whether the number can be divisible by all its digits, we also need the value of this number. Obviously, it is impossible to record the value. In fact, we only need to record its Modulo for 2520, in this way, we can design the following digital DP: dfs (pos, mod, lcm, f), pos is the current BIT, mod is the model of the first bit to 2520, lcm is the minimum public multiple of the preceding digits, and f indicates whether the preceding digits have reached the upper limit. As a result, the dp array will be opened to 19*2520*2520, which obviously exceeds the memory, considering that the minimum public multiples are discrete, the values of 1-2520 may be the least public multiples. There are only 48 instances. After discretization, the last dimension of the dp array can be reduced to 48. */# Include <stdio. h> # include <string. h >#include <iostream >#include <algorithm> using namespace std; const int mod = 2520; typedef long LL; LL dp [25] [mod] [50]; int index [2530], bit [25]; int gcd (int a, int B) {if (B = 0) return a; else return gcd (B, a % B);} int lcm (int a, int B) {return a/gcd (a, B) * B;} void init () {int num = 0; for (int I = 1; I <= mod; I ++) {if (mod % I = 0) index [I] = num ++ ;}} LL dfs (int pos, int presum, int Prelcm, bool flag) {if (pos =-1) return presum % prelcm = 0; if (! Flag & dp [pos] [presum] [index [prelcm]! =-1) return dp [pos] [presum] [index [prelcm]; LL ans = 0; int end = flag? Bit [pos]: 9; for (int I = 0; I <= end; I ++) {int nowsum = (presum * 10 + I) % mod; int nowlcm = prelcm; if (I) nowlcm = lcm (nowlcm, I); ans + = dfs (pos-1, nowsum, nowlcm, flag & I = end );} if (! Flag) dp [pos] [presum] [index [prelcm] = ans; return ans;} LL solve (LL x) {int len = 0; while (x) {bit [len ++] = x % 10; x/= 10;} return dfs (len-1, 0, 1);} int main () {int T; init (); memset (dp,-1, sizeof (dp); scanf ("% d", & T); while (T --) {LL r, l; scanf ("% I64d % I64d", & l, & r); printf ("% I64d \ n", solve (r)-solve L-1 ));} return 0 ;}