Topic links
Test instructions: Defines "beautiful number" as one digit n divisible by all digits not 0
Analysis: That is, N is a multiple of the least common multiple of all digits of a digital position. LCM (1 to 9) = 2520. n satisfies is a multiple of the approximate 2520. DP[LEN][VAL][LCM] One-dimensional number of digits, one-dimensional value of%2520 (the preservation of the original number is not possible, there is no need, 2520 is feasible least common multiple the largest one), one-dimensional for the current digital LCM, to determine the conditions of satisfaction is val%lcm==0. This problem discretized 2520 of the approximate, otherwise the space will not open.
#include <bits/stdc++.h>typedef long Long ll;ll dp[20][2520][50];int digit[20];int id[2521];int tot;int GCD (int A, int b) {return B? GCD (b, a% B): A;} int LCM (int a, int b) {return A/GCD (A, b) * b;} void Init_lcm () {tot = 0; for (int i=1; i<=2520; ++i) {if (2520% i = = 0) {Id[i] = ++tot; }}}ll DFS (int pos, int val, int LCM, bool limit) {if (pos = =-1) {return val% LCM = = 0; } ll &now = DP[POS][VAL][ID[LCM]]; if (!limit && Now! =-1) {return now; } int d = limit? Digit[pos]: 9; ll ret = 0; for (int i=0; i<=d; ++i) {int tval = (val * + i)% 2520; int TLCM = I? LCM/GCD (LCM, i) * I:LCM; RET + = DFS (pos-1, Tval, TLCM, limit && i = = d); } if (!limit) {now = ret; } return ret;} ll solve (ll x) {int len = 0; if (x = = 0) {digit[len++] = 0; } else {while (x) {digit[len++] = x% 10; x/= 10; }} return DFS (len-1, 0, 1, true);} int main () {INIT_LCM (); Memset (DP,-1, sizeof (DP)); int T; scanf ("%d", &t); while (t--) {ll L, R; Std::cin >> L >> R; Std::cout << Solve (R)-Solve (l-1) << ' \ n '; } return 0;}
Digital DP CF 55D Beautiful numbers