Codeforces 55D Beautiful Number, codeforces55d
Codeforces 55D Beautiful Number
A positive integer number is beautiful if and only if it is divisibleEach of its nonzero digits.
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 ≤ limitRILimit ≤ limit 9 Limit · 1018 ).
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
1
1 9
Output
9
Input
1
12 15
Output
2
Ideas:
For more information about the code, see AC_Von.
# Include <bits/stdc ++. h> using namespace std; # define rep (I, n) for (int (I) = 0; I <(n); I ++) # define MS0 () memset (a, 0, sizeof (a) # define MS1 (a) memset (a,-1, sizeof (a) typedef long ll; const int MOD = 2520; ll dp [21] [1, 2520] [50]; int d [21], index [MOD + 5]; void init () {for (int I = 1, tot = 0; I <= MOD; I ++) if (MOD % I = 0) index [I] = tot ++; MS1 (dp );} int lcm (int a, int B) {return a/_ gcd (a, B) * B;} ll dfs (int pos, int Prev, int prelcm, int edge) {if (pos =-1) return prev % prelcm?; // *** Ll ans = dp [pos] [prev] [index [prelcm]; if (! Edge &&~ Ans) return ans; ans = 0; int e = edge? D [pos]: 9; for (int I = 0; I <= e; I ++) {int nowlcm = I? Lcm (prelcm, I): prelcm; int nowv = (prev * 10 + I) % MOD; ans + = dfs (pos-1, nowv, nowlcm, edge & I = e) ;}if (! Edge) dp [pos] [prev] [index [prelcm] = ans; return ans;} ll query (ll n) {MS0 (d); int tot = 0; while (n) {d [tot ++] = n % 10; n/= 10;} return dfs (tot-1, 0, 1, 1);} int main () {init (); int T; cin> T; while (T --) {ll l, r; scanf ("% I64d % I64d", & l, & r ); printf ("% I64d \ n", query (r)-L-1 ));}}View Code