#include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define REP(i, n) for (int i = 0; i < (n); ++i) #define FWD(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int32_t i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) #define what(x) cerr << #x << " is " << x << endl; #define int long long ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) // void add(vector<int> &A, const vector<int> B) { rep(i, sz(B)) A[i] += B[i]; } // vector<int> shift(vector<int> A, int offset) { // int x = sz(A); // offset = offset % x; // vector<int> res(x); // rep(i, x) res[(i + offset) % x] = A[i]; // return res; // } const int MAXK = 19; const int MAXW = 40; const int NWW = 2520; const int MASK = 512; int pot10[MAXK]; int arr[MAXW + 1][MASK][NWW]; int tmp[MASK][NWW]; vector<int> idxs(MAXW); bool is_pot(int num) { rep(i, MAXK) if (num == pot10[i]) return true; return false; } void clear(int l) { rep(i, MASK) rep(j, NWW) arr[l][i][j] = 0; } void set_empty(int l) { arr[l][0][0] = 1; } int get_idx() { debug(idxs.size()); int l = idxs.back(); idxs.pop_back(); clear(l); return l; } void zwroc(int l) { idxs.push_back(l); } int shift(int l, int k, int val) { --k; val %= NWW; clear(MAXW); rep(S, MASK) { if (S & (1 << k)) rep(i, NWW) arr[MAXW][S][(i + val) >= NWW ? i + val - NWW : i + val] = arr[l][S][i] + arr[l][S ^ (1 << k)][i]; } return MAXW; } void add(int l, int other) { rep(S, MASK) rep(i, NWW) arr[l][S][i] += arr[other][S][i]; } int get_nww(int S) { int nww = 1; for (int i = 1; i <= 9; i++) if (S & (1 << (i - 1))) nww = lcm(nww, i); return nww; } int get_res(int l) { int res = 0; for (int S = 1; S < MASK; S++) { int nww = get_nww(S); for (int32_t i = 0; i < NWW; i += nww) res += arr[l][S][i]; } return res; } struct Mapa { map<pair<int, int>, int> memo; Mapa() { memo[{0, -1}] = get_idx(); set_empty(memo[{0, -1}]); } void erase(pii X) { auto l = memo[X]; zwroc(l); memo.erase(X); } int get(int num, int poziom) { debug(num, poziom); auto r = memo.find({num, poziom}); if (r != memo.end()) return r->nd; int k = num / pot10[poziom]; get(pot10[poziom] - 1, poziom - 1); if (k > 0) { get(num - pot10[poziom] * k, poziom - 1); } memo[{num, poziom}] = get_idx(); int res = memo[{num, poziom}]; int last = get(pot10[poziom] - 1, poziom - 1); for (int l = 1; l < k; l++) add(res, shift(last, l, pot10[poziom] * l)); if (k > 0) { last = get(num - pot10[poziom] * k, poziom - 1); add(res, shift(last, k, pot10[poziom] * k)); if (poziom != 0 && !is_pot(num - pot10[poziom] * k + 1)) { erase({num - pot10[poziom] * k, poziom - 1}); } } return memo[{num, poziom}]; } }; Mapa mapa; int find_poziom(int num) { for (int i = MAXK - 1; i >= 0; i--) if (num >= pot10[i]) return i; } int solve(int num) { if (num == 0) return 0; int res = 0; int poziom = find_poziom(num); debug(res, poziom); auto R = mapa.get(num, poziom); res += get_res(R); debug(res, poziom); for (int i = 0; i < poziom; i++) { R = mapa.get(pot10[i + 1] - 1, i); res += get_res(R); } debug(res); return res; } void pre() { pot10[0] = 1; for (int i = 1; i < MAXK; i++) pot10[i] = 10 * pot10[i - 1]; iota(all(idxs), 0); } int32_t main() { _upgrade; pre(); int l, r; cin >> l >> r; int resr = solve(r); int resl = solve(l - 1); debug(resr, resl); debug(mapa.memo.size()); cout << resr - resl << 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define REP(i, n) for (int i = 0; i < (n); ++i) #define FWD(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int32_t i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) #define what(x) cerr << #x << " is " << x << endl; #define int long long ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) // void add(vector<int> &A, const vector<int> B) { rep(i, sz(B)) A[i] += B[i]; } // vector<int> shift(vector<int> A, int offset) { // int x = sz(A); // offset = offset % x; // vector<int> res(x); // rep(i, x) res[(i + offset) % x] = A[i]; // return res; // } const int MAXK = 19; const int MAXW = 40; const int NWW = 2520; const int MASK = 512; int pot10[MAXK]; int arr[MAXW + 1][MASK][NWW]; int tmp[MASK][NWW]; vector<int> idxs(MAXW); bool is_pot(int num) { rep(i, MAXK) if (num == pot10[i]) return true; return false; } void clear(int l) { rep(i, MASK) rep(j, NWW) arr[l][i][j] = 0; } void set_empty(int l) { arr[l][0][0] = 1; } int get_idx() { debug(idxs.size()); int l = idxs.back(); idxs.pop_back(); clear(l); return l; } void zwroc(int l) { idxs.push_back(l); } int shift(int l, int k, int val) { --k; val %= NWW; clear(MAXW); rep(S, MASK) { if (S & (1 << k)) rep(i, NWW) arr[MAXW][S][(i + val) >= NWW ? i + val - NWW : i + val] = arr[l][S][i] + arr[l][S ^ (1 << k)][i]; } return MAXW; } void add(int l, int other) { rep(S, MASK) rep(i, NWW) arr[l][S][i] += arr[other][S][i]; } int get_nww(int S) { int nww = 1; for (int i = 1; i <= 9; i++) if (S & (1 << (i - 1))) nww = lcm(nww, i); return nww; } int get_res(int l) { int res = 0; for (int S = 1; S < MASK; S++) { int nww = get_nww(S); for (int32_t i = 0; i < NWW; i += nww) res += arr[l][S][i]; } return res; } struct Mapa { map<pair<int, int>, int> memo; Mapa() { memo[{0, -1}] = get_idx(); set_empty(memo[{0, -1}]); } void erase(pii X) { auto l = memo[X]; zwroc(l); memo.erase(X); } int get(int num, int poziom) { debug(num, poziom); auto r = memo.find({num, poziom}); if (r != memo.end()) return r->nd; int k = num / pot10[poziom]; get(pot10[poziom] - 1, poziom - 1); if (k > 0) { get(num - pot10[poziom] * k, poziom - 1); } memo[{num, poziom}] = get_idx(); int res = memo[{num, poziom}]; int last = get(pot10[poziom] - 1, poziom - 1); for (int l = 1; l < k; l++) add(res, shift(last, l, pot10[poziom] * l)); if (k > 0) { last = get(num - pot10[poziom] * k, poziom - 1); add(res, shift(last, k, pot10[poziom] * k)); if (poziom != 0 && !is_pot(num - pot10[poziom] * k + 1)) { erase({num - pot10[poziom] * k, poziom - 1}); } } return memo[{num, poziom}]; } }; Mapa mapa; int find_poziom(int num) { for (int i = MAXK - 1; i >= 0; i--) if (num >= pot10[i]) return i; } int solve(int num) { if (num == 0) return 0; int res = 0; int poziom = find_poziom(num); debug(res, poziom); auto R = mapa.get(num, poziom); res += get_res(R); debug(res, poziom); for (int i = 0; i < poziom; i++) { R = mapa.get(pot10[i + 1] - 1, i); res += get_res(R); } debug(res); return res; } void pre() { pot10[0] = 1; for (int i = 1; i < MAXK; i++) pot10[i] = 10 * pot10[i - 1]; iota(all(idxs), 0); } int32_t main() { _upgrade; pre(); int l, r; cin >> l >> r; int resr = solve(r); int resl = solve(l - 1); debug(resr, resl); debug(mapa.memo.size()); cout << resr - resl << endl; } |