#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; const int MOD = 2520; ll dp[18][1 << 9][MOD]; vector<int> nww(1 << 9); vector<int> pot10(18); int NWW(int a, int b) { return a * b / __gcd(a, b); } string NaStr(ll x) { if (x == 0) return "0"; string w = ""; while (x != 0) { w = (char) (x % 10 + '0') + w; x /= 10; } return w; } bool Ok(ll x) { if (x == 0) return false; string y = NaStr(x); for (int i = 0; i < y.size(); ++i) if (y[i] == '0' || x % (y[i] - '0') != 0) return false; return true; } bool Ok(string x) { if (x == "0") return false; ll y = 0; for (int i = 0; i < x.size(); ++i) y = (y * (ll) 10 + (x[i] - '0')); for (int i = 0; i < x.size(); ++i) if (x[i] == '0' || y % (x[i] - '0') != 0) return false; return true; } ll Ile(string x) { int n = x.size(); ll odp = 0; for (int i = 1; i < n; ++i) for (int mask = 1; mask < (1 << 9); ++mask) for (int r = 0; r < MOD; r += nww[mask]) odp += dp[i - 1][mask][r]; int maskPref = 0, reszPref = 0; for (int i = 0; i < n; ++i) { if (x[i] == '0') break; for (int c = 1; c < x[i] - '0'; ++c) { if (i != n - 1) { int maskPref2 = (maskPref | (1 << (c - 1))); int reszPref2 = (reszPref * 10 + c) % MOD; for (int mask = 1; mask < (1 << 9); ++mask) { int start = -(pot10[n - i - 1] * reszPref2) % nww[maskPref2 | mask]; if (start < 0) start += nww[maskPref2 | mask]; for (int r = start; r < MOD; r += nww[maskPref2 | mask]) odp += dp[n - i - 2][mask][r]; } } else { string t = x; t[i] = (char) (c + '0'); odp += Ok(t); } } maskPref |= (1 << (x[i] - '0' - 1)); reszPref = (reszPref * 10 + (x[i] - '0')) % MOD; } return odp; } int main() { ios_base::sync_with_stdio(0); ll l, r; cin >> l >> r; pot10[0] = 1; for (int i = 1; i < 18; ++i) pot10[i] = (pot10[i - 1] * 10) % MOD; for (int mask = 1; mask < (1 << 9); ++mask) { nww[mask] = 1; for (int i = 0; i < 9; ++i) if ((mask & (1 << i)) > 0) nww[mask] = NWW(nww[mask], i + 1); } for (int i = 1; i <= 9; ++i) dp[0][1 << (i - 1)][i] = 1; for (int i = 0; i < 17; ++i) for (int mask = 1; mask < (1 << 9); ++mask) for (int r = 0; r < MOD; ++r) for (int c = 1; c <= 9; ++c) dp[i + 1][mask | (1 << (c - 1))][(c * pot10[i + 1] + r) % MOD] += dp[i][mask][r]; cout << (Ile(NaStr(r)) + Ok(r)) - (Ile(NaStr(l - 1)) + Ok(l - 1)); return 0; }
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; const int MOD = 2520; ll dp[18][1 << 9][MOD]; vector<int> nww(1 << 9); vector<int> pot10(18); int NWW(int a, int b) { return a * b / __gcd(a, b); } string NaStr(ll x) { if (x == 0) return "0"; string w = ""; while (x != 0) { w = (char) (x % 10 + '0') + w; x /= 10; } return w; } bool Ok(ll x) { if (x == 0) return false; string y = NaStr(x); for (int i = 0; i < y.size(); ++i) if (y[i] == '0' || x % (y[i] - '0') != 0) return false; return true; } bool Ok(string x) { if (x == "0") return false; ll y = 0; for (int i = 0; i < x.size(); ++i) y = (y * (ll) 10 + (x[i] - '0')); for (int i = 0; i < x.size(); ++i) if (x[i] == '0' || y % (x[i] - '0') != 0) return false; return true; } ll Ile(string x) { int n = x.size(); ll odp = 0; for (int i = 1; i < n; ++i) for (int mask = 1; mask < (1 << 9); ++mask) for (int r = 0; r < MOD; r += nww[mask]) odp += dp[i - 1][mask][r]; int maskPref = 0, reszPref = 0; for (int i = 0; i < n; ++i) { if (x[i] == '0') break; for (int c = 1; c < x[i] - '0'; ++c) { if (i != n - 1) { int maskPref2 = (maskPref | (1 << (c - 1))); int reszPref2 = (reszPref * 10 + c) % MOD; for (int mask = 1; mask < (1 << 9); ++mask) { int start = -(pot10[n - i - 1] * reszPref2) % nww[maskPref2 | mask]; if (start < 0) start += nww[maskPref2 | mask]; for (int r = start; r < MOD; r += nww[maskPref2 | mask]) odp += dp[n - i - 2][mask][r]; } } else { string t = x; t[i] = (char) (c + '0'); odp += Ok(t); } } maskPref |= (1 << (x[i] - '0' - 1)); reszPref = (reszPref * 10 + (x[i] - '0')) % MOD; } return odp; } int main() { ios_base::sync_with_stdio(0); ll l, r; cin >> l >> r; pot10[0] = 1; for (int i = 1; i < 18; ++i) pot10[i] = (pot10[i - 1] * 10) % MOD; for (int mask = 1; mask < (1 << 9); ++mask) { nww[mask] = 1; for (int i = 0; i < 9; ++i) if ((mask & (1 << i)) > 0) nww[mask] = NWW(nww[mask], i + 1); } for (int i = 1; i <= 9; ++i) dp[0][1 << (i - 1)][i] = 1; for (int i = 0; i < 17; ++i) for (int mask = 1; mask < (1 << 9); ++mask) for (int r = 0; r < MOD; ++r) for (int c = 1; c <= 9; ++c) dp[i + 1][mask | (1 << (c - 1))][(c * pot10[i + 1] + r) % MOD] += dp[i][mask][r]; cout << (Ile(NaStr(r)) + Ok(r)) - (Ile(NaStr(l - 1)) + Ok(l - 1)); return 0; } |