[Codeforces 55] D. Beautiful numbers

Source: Internet
Author: User
Tags gcd greatest common divisor

[Codeforces 55] D. Beautiful numbers

Question Description

Volodya is a odd boy and his taste are strange as well. It seems to him. A positive integer number isBeautiful if and only if it are divisible by each of its nonzero digits. We 'll not be 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 cases T (1?≤? T? ≤?10). Each of the next t lines contains II natural numbers li and R I (1?≤?) L i? ≤? R i? ≤?9? (Ten).

%lld specificator to read or write 64-bit integers in C + +. It is the preffered to use cin (also-use %i64d).

Output

Output should contain T numbers-answers to the queries, one number per line-quantities of beautiful Numbe RS in given intervals (from li to Ri, inclusively).

Input example

2 1 9  A  the

Output example

9 2

Data size and conventions

See " Input "

Exercises

"Divisible by numbers on all digits" is equivalent to "divisible by greatest common divisor of all digits." The f[i][k][m] represents the number of an I-bit, and the least common multiple of all numbers equals K, and this number is M. Wait, status m knows this number, what's DP? Take it easy, we'll optimize it one step at a pace.

We know that LCM (1, 2, 3, 4, 5, 6, 7, 8, 9) = 2520 (that is, 1~9 's least common multiple), then it is not necessary to be too big for the one-dimensional state of the m just now, and this dimension state is good for 2520, that is, m represents the value of this number to mod 2520.

Also, 1~9 these numbers, all of the possible least common multiple are only 48 (this might be the reader's own try), so the discrete wave K becomes the maximum of only 48.

The last state number is approximately: 19 * 48 * 2520 = 2298240.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype > #include <algorithm>using namespace std; #define LL Long Longll Read () {ll x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = GetChar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 21#define maxm 2521#define maxc 49int LCM[MAXC], cl, ID[MAXM], Gcd[maxm][maxm];bool TMP[MAXM]; LL F[MAXN][MAXC][MAXM], ten[maxn];int gcd (int x, int y) {return!y? x:gcd (y, x% y);}  int get (int l, int x) {int nl;if (!x &&!l) nl = 0;if (x && l) nl = L * x/gcd[l][x];if (!x && L) NL = l;if (x &&!l) nl = X;return nl;} int NUM[MAXN]; ll sum (ll x) {int cnt = 0; LL tx = X;while (x) num[++cnt] = x%, x/= 10; LL ans = 0;  int L = 1;for (int i = cnt; i; i--) {for (int j = 0; J < Num[i]; J + +) {int nl = Get (L, J); for (int k = 0; k <= cl; k++) {int NNL = Get (NL, lcm[k]); LL t;if (i < cnt) T = (tx/ten[i] * Ten[i] + ten[i-1] * j)% 2520;else t = ten[i-1] * J% 2520;for (int m = 0; m < maxm-1; m + = NNL) {int m = ( M-t + 2520)% 2520; if (M < 0) continue;if (F[i-1][k][m]) ans + = F[i-1][k][m];}} L = Get (L, Num[i]);} if (tx% L = = 0) ans++;return ans;} int main () {for (int i = 1; i < MAXM; i++) for (int j = 1; j < Maxm; J + +) Gcd[i][j] = GCD (i, j); tmp[1] = 1;for (int j = 2; J <= 9; J + +) for (int x = maxm-1; x; x--) if (tmp[x]) tmp[x*j/gcd[x][j] = 1;for (int i = 1; i < MAXM; i++) if (Tmp[i]) LCM[++CL] = I, id[i] = cl;ten[0] = 1;for (int i = 1; i < MAXN; i++) ten[i] = ten[i-1] * 10;f[0][0][0] = 1;for (int j = 0; J <= 9; J + +) F[1][id[j]][j] = 1;for (int i = 1; i < maxn-1; i++) for (int k = 0; k <= cl; k++) for (int m = 0; m < MAXM; m++ if (F[i][k][m]) {int L = lcm[k];for (int x = 0; x <= 9; + +) {int nl = get (l, x); f[i+1][id[nl]][(ten[i]*x+m)%2520] + = f I K [M];}} int T = Read (), while (t--) {LL L = read (), r = Read ();p rintf ("%i64d\n", SUM (r)-sum (L-1));} return 0;}

[Codeforces 55] D. Beautiful numbers

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.