#include <bits/stdc++.h> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using PII = pair<int,int>; using PLL = pair<LL,LL>; using VI = vector<int>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } const int inf = 1e9; const LL infl = 1e18; const int M = 5 * 7 * 8 * 9; VI a; LL dp[20][2][512][M]; LL solve(LL n) { if (!n) return 0; a.clear(); while (n) { a.PB(n % 10); n /= 10; } a.PB(0); reverse(ALL(a)); memset(dp, 0, sizeof(dp)); FOR(d, 1, a[1]) dp[1][d < a[1]][1 << (d - 1)][d] = 1; FOR(i, 1, sz(a) - 2) { FOR(f, 0, 1) FOR(d, 1, f ? 9 : a[i + 1]) FOR(m, 1, 511) FOR(x, 0, M - 1) { dp[i + 1][f | (d < a[i + 1])][m | (1 << (d - 1))][(10 * x + d) % M] += dp[i][f][m][x]; } FOR(d, 1, 9) dp[i + 1][1][1 << (d - 1)][d]++; } LL ans = 0; FOR(f, 0, 1) FOR(m, 1, 511) FOR(x, 0, M - 1) { bool ok = true; FOR(d, 1, 9) ok &= (!(m & (1 << (d - 1))) || !(x % d)); ans += ok * dp[sz(a) - 1][f][m][x]; } return ans; } int main() { LL l, h; scanf("%lld %lld", &l, &h); LL ans = solve(h) - solve(l - 1); printf("%lld\n", ans); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <bits/stdc++.h> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using PII = pair<int,int>; using PLL = pair<LL,LL>; using VI = vector<int>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } const int inf = 1e9; const LL infl = 1e18; const int M = 5 * 7 * 8 * 9; VI a; LL dp[20][2][512][M]; LL solve(LL n) { if (!n) return 0; a.clear(); while (n) { a.PB(n % 10); n /= 10; } a.PB(0); reverse(ALL(a)); memset(dp, 0, sizeof(dp)); FOR(d, 1, a[1]) dp[1][d < a[1]][1 << (d - 1)][d] = 1; FOR(i, 1, sz(a) - 2) { FOR(f, 0, 1) FOR(d, 1, f ? 9 : a[i + 1]) FOR(m, 1, 511) FOR(x, 0, M - 1) { dp[i + 1][f | (d < a[i + 1])][m | (1 << (d - 1))][(10 * x + d) % M] += dp[i][f][m][x]; } FOR(d, 1, 9) dp[i + 1][1][1 << (d - 1)][d]++; } LL ans = 0; FOR(f, 0, 1) FOR(m, 1, 511) FOR(x, 0, M - 1) { bool ok = true; FOR(d, 1, 9) ok &= (!(m & (1 << (d - 1))) || !(x % d)); ans += ok * dp[sz(a) - 1][f][m][x]; } return ans; } int main() { LL l, h; scanf("%lld %lld", &l, &h); LL ans = solve(h) - solve(l - 1); printf("%lld\n", ans); } |