Codeforces 55D Beautiful Number, codeforces55d

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.