#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <random> #include <bitset> #include <numeric> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int L = 8; const int N = 21; const int M = 1 << L; const int LCM = 2520; ll dp[N][M][LCM]; int vals[M]; int pot[N]; int val(int maska) { int lcm = 1; rep(i, 0, L) { if (maska & (1 << i)) { int g = gcd(lcm, i + 2); lcm = lcm / g * (i + 2); } } return lcm; } void precompute() { rep(mask, 0, M) { vals[mask] = val(mask); } dp[0][0][0] = 1; pot[0] = 1; rep(i, 1, N) { pot[i] = (pot[i-1] * 10) % LCM; } int mn = 1; rep(dl, 0, N - 2) { rep(maska, 0, M) { rep(reszta, 0, LCM) { if (dp[dl][maska][reszta] == 0) { continue; } rep(nowa_cyfra, 1, 10) { int nmaska = maska; if (nowa_cyfra > 1) { nmaska |= (1 << (nowa_cyfra - 2)); } int nreszta = (reszta + mn * nowa_cyfra) % LCM; dp[dl + 1][nmaska][nreszta] += dp[dl][maska][reszta]; } } } mn = (mn * 10) % LCM; } } bool inMask(int x, int mask) { if (x == 1) return true; return (mask & (1<<(x - 2))) > 0; } ll res(ll x) { if (x == 0) return 0; ll wyn = 0; string xx = to_string(x); ford(dl, sz(xx), 1) { rep(maska, 0, M) rep(reszta, 0, LCM) if (reszta % vals[maska] == 0) { if (dp[dl][maska][reszta] == 0) { continue; } wyn += dp[dl][maska][reszta]; } } int pref = 0; int pref_reszta = 0; ford(dl, sz(xx), 0) { int cyf = xx[sz(xx) - dl - 1] - '0'; if (cyf == 0) { break; } rep(nowa_cyfra, 1, cyf) { rep(maska, 0, M) { int nmaska = maska | pref; if (nowa_cyfra > 1) { nmaska |= (1 << (nowa_cyfra - 2)); } rep(reszta, 0, LCM) { int nreszta = (pref_reszta + reszta + nowa_cyfra * pot[dl]) % LCM; if (nreszta % vals[nmaska] == 0) { wyn += dp[dl][maska][reszta]; } } } } if (cyf > 1) { pref |= (1 << (cyf - 2)); } pref_reszta = (pref_reszta + pot[dl]*cyf) % LCM; } return wyn; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); precompute(); ll l, r; cin >> l >> r; cout << res(r + 1) - res(l) << endl; }
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <random> #include <bitset> #include <numeric> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int L = 8; const int N = 21; const int M = 1 << L; const int LCM = 2520; ll dp[N][M][LCM]; int vals[M]; int pot[N]; int val(int maska) { int lcm = 1; rep(i, 0, L) { if (maska & (1 << i)) { int g = gcd(lcm, i + 2); lcm = lcm / g * (i + 2); } } return lcm; } void precompute() { rep(mask, 0, M) { vals[mask] = val(mask); } dp[0][0][0] = 1; pot[0] = 1; rep(i, 1, N) { pot[i] = (pot[i-1] * 10) % LCM; } int mn = 1; rep(dl, 0, N - 2) { rep(maska, 0, M) { rep(reszta, 0, LCM) { if (dp[dl][maska][reszta] == 0) { continue; } rep(nowa_cyfra, 1, 10) { int nmaska = maska; if (nowa_cyfra > 1) { nmaska |= (1 << (nowa_cyfra - 2)); } int nreszta = (reszta + mn * nowa_cyfra) % LCM; dp[dl + 1][nmaska][nreszta] += dp[dl][maska][reszta]; } } } mn = (mn * 10) % LCM; } } bool inMask(int x, int mask) { if (x == 1) return true; return (mask & (1<<(x - 2))) > 0; } ll res(ll x) { if (x == 0) return 0; ll wyn = 0; string xx = to_string(x); ford(dl, sz(xx), 1) { rep(maska, 0, M) rep(reszta, 0, LCM) if (reszta % vals[maska] == 0) { if (dp[dl][maska][reszta] == 0) { continue; } wyn += dp[dl][maska][reszta]; } } int pref = 0; int pref_reszta = 0; ford(dl, sz(xx), 0) { int cyf = xx[sz(xx) - dl - 1] - '0'; if (cyf == 0) { break; } rep(nowa_cyfra, 1, cyf) { rep(maska, 0, M) { int nmaska = maska | pref; if (nowa_cyfra > 1) { nmaska |= (1 << (nowa_cyfra - 2)); } rep(reszta, 0, LCM) { int nreszta = (pref_reszta + reszta + nowa_cyfra * pot[dl]) % LCM; if (nreszta % vals[nmaska] == 0) { wyn += dp[dl][maska][reszta]; } } } } if (cyf > 1) { pref |= (1 << (cyf - 2)); } pref_reszta = (pref_reszta + pot[dl]*cyf) % LCM; } return wyn; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); precompute(); ll l, r; cin >> l >> r; cout << res(r + 1) - res(l) << endl; } |