/* _______ _,,ad8888888888bba,_ ,ad88888I888888888888888ba, ,88888888I88888888888888888888a, ,d888888888I8888888888888888888888b, d88888PP"""" ""YY88888888888888888888b, ,d88"'__,,--------,,,,.;ZZZY8888888888888, ,8IIl'" ;;l"ZZZIII8888888888, ,I88l;' ;lZZZZZ888III8888888, ,II88Zl;. ;llZZZZZ888888I888888, ,II888Zl;. .;;;;;lllZZZ888888I8888b ,II8888Z;; `;;;;;''llZZ8888888I8888, II88888Z;' .;lZZZ8888888I888b II88888Z; _,aaa, .,aaaaa,__.l;llZZZ88888888I888 II88888IZZZZZZZZZ, .ZZZZZZZZZZZZZZ;llZZ88888888I888, II88888IZZ<'(@@>Z| |ZZZ<'(@@>ZZZZ;;llZZ888888888I88I ,II88888; `""" ;| |ZZ; `""" ;;llZ8888888888I888 II888888l `;; .;llZZ8888888888I888, ,II888888Z; ;;; .;;llZZZ8888888888I888I III888888Zl; .., `;; ,;;lllZZZ88888888888I888 II88888888Z;;...;(_ _) ,;;;llZZZZ88888888888I888, II88888888Zl;;;;;' `--'Z;. .,;;;;llZZZZ88888888888I888b ]I888888888Z;;;;' ";llllll;..;;;lllZZZZ88888888888I8888, II888888888Zl.;;"Y88bd888P";;,..;lllZZZZZ88888888888I8888I II8888888888Zl;.; `"PPP";;;,..;lllZZZZZZZ88888888888I88888 II888888888888Zl;;. `;;;l;;;;lllZZZZZZZZW88888888888I88888 `II8888888888888Zl;. ,;;lllZZZZZZZZWMZ88888888888I88888 II8888888888888888ZbaalllZZZZZZZZZWWMZZZ8888888888I888888, `II88888888888888888b"WWZZZZZWWWMMZZZZZZI888888888I888888b `II88888888888888888;ZZMMMMMMZZZZZZZZllI888888888I8888888 `II8888888888888888 `;lZZZZZZZZZZZlllll888888888I8888888, II8888888888888888, `;lllZZZZllllll;;.Y88888888I8888888b, ,II8888888888888888b .;;lllllll;;;.;..88888888I88888888b, II888888888888888PZI;. .`;;;.;;;..; ...88888888I8888888888, II888888888888PZ;;';;. ;. .;. .;. .. Y8888888I88888888888b, ,II888888888PZ;;' `8888888I8888888888888b, II888888888' 888888I8888888888888888b ,II888888888 ,888888I88888888888888888 ,d88888888888 d888888I8888888888ZZZZZZZ ,ad888888888888I 8888888I8888ZZZZZZZZZZZZZ ,d888888888888888' 888888IZZZZZZZZZZZZZZZZZZ ,d888888888888P'8P' Y888ZZZZZZZZZZZZZZZZZZZZZ ,8888888888888, " ,ZZZZZZZZZZZZZZZZZZZZZZZZ d888888888888888, ,ZZZZZZZZZZZZZZZZZZZZZZZZZZZ 888888888888888888a, _ ,ZZZZZZZZZZZZZZZZZZZZ888888888 888888888888888888888ba,_d' ,ZZZZZZZZZZZZZZZZZ88888888888888 8888888888888888888888888888bbbaaa,,,______,ZZZZZZZZZZZZZZZ888888888888888888 88888888888888888888888888888888888888888ZZZZZZZZZZZZZZZ888888888888888888888 8888888888888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888888888 888888888888888888888888888888888888888ZZZZZZZZZZZZZZ888888888888888888888888 8888888888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888888888888 88888888888888888888888888888888888ZZZZZZZZZZZZZZ8888888888888888888888888888 8888888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888 Normand 88 88888888888888888888888888888888ZZZZZZZZZZZZZZ8888888888888888888 Veilleux 88 8888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888888888888888888 https://asciiart.website/index.php?art=art%20and%20design/mona%20lisa*/ #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef uint32_t ul; typedef int32_t l; typedef uint64_t ull; typedef int64_t ll; template <typename T> using statistics_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const l INF = 1000000000 + 9; const ll MOD = 2520; const l PRIME = 200003; const ll L_INF = 1000000000000000000LL + 7; const double EPS = 1e-5; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end() #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second ll lf, rt; ll dp[20][MOD][512][2]; //prefix | modulo | mask | smaller vector<l> num; ll calc(ll v) { if(v == 0) return 0; num.clear(); while(v) { num.PB(v%10LL); v /= 10LL; } //pos = 0 REP(2520, mod) FOR(1, 512, mask) { bool err{false}; FORE(1, 9, d) if(mask & (1 << (d-1)) && (mod % d != 0)) { err = true; break; } dp[0][mod][mask][0] = dp[0][mod][mask][1] = 1-err; } FORE(1, num.size(), pos) REP(2520, mod) REP(512, mask) REP(2, smaller) { l limit; if(smaller == 0) limit = num[pos-1]; else limit = 9; dp[pos][mod][mask][smaller] = 0; FORE(1, limit, d) { bool n_smaller{smaller}; if(d < limit) n_smaller = 1; dp[pos][mod][mask][smaller] += dp[pos-1][(mod*10+d)%MOD][mask | (1 << (d-1))][n_smaller]; } } ll res{0}; FORE(1, num.size()-1, i) res += dp[i][0][0][1]; res += dp[num.size()][0][0][0]; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> lf >> rt; cout << calc(rt) - calc(lf-1) << "\n"; }
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 | /* _______ _,,ad8888888888bba,_ ,ad88888I888888888888888ba, ,88888888I88888888888888888888a, ,d888888888I8888888888888888888888b, d88888PP"""" ""YY88888888888888888888b, ,d88"'__,,--------,,,,.;ZZZY8888888888888, ,8IIl'" ;;l"ZZZIII8888888888, ,I88l;' ;lZZZZZ888III8888888, ,II88Zl;. ;llZZZZZ888888I888888, ,II888Zl;. .;;;;;lllZZZ888888I8888b ,II8888Z;; `;;;;;''llZZ8888888I8888, II88888Z;' .;lZZZ8888888I888b II88888Z; _,aaa, .,aaaaa,__.l;llZZZ88888888I888 II88888IZZZZZZZZZ, .ZZZZZZZZZZZZZZ;llZZ88888888I888, II88888IZZ<'(@@>Z| |ZZZ<'(@@>ZZZZ;;llZZ888888888I88I ,II88888; `""" ;| |ZZ; `""" ;;llZ8888888888I888 II888888l `;; .;llZZ8888888888I888, ,II888888Z; ;;; .;;llZZZ8888888888I888I III888888Zl; .., `;; ,;;lllZZZ88888888888I888 II88888888Z;;...;(_ _) ,;;;llZZZZ88888888888I888, II88888888Zl;;;;;' `--'Z;. .,;;;;llZZZZ88888888888I888b ]I888888888Z;;;;' ";llllll;..;;;lllZZZZ88888888888I8888, II888888888Zl.;;"Y88bd888P";;,..;lllZZZZZ88888888888I8888I II8888888888Zl;.; `"PPP";;;,..;lllZZZZZZZ88888888888I88888 II888888888888Zl;;. `;;;l;;;;lllZZZZZZZZW88888888888I88888 `II8888888888888Zl;. ,;;lllZZZZZZZZWMZ88888888888I88888 II8888888888888888ZbaalllZZZZZZZZZWWMZZZ8888888888I888888, `II88888888888888888b"WWZZZZZWWWMMZZZZZZI888888888I888888b `II88888888888888888;ZZMMMMMMZZZZZZZZllI888888888I8888888 `II8888888888888888 `;lZZZZZZZZZZZlllll888888888I8888888, II8888888888888888, `;lllZZZZllllll;;.Y88888888I8888888b, ,II8888888888888888b .;;lllllll;;;.;..88888888I88888888b, II888888888888888PZI;. .`;;;.;;;..; ...88888888I8888888888, II888888888888PZ;;';;. ;. .;. .;. .. Y8888888I88888888888b, ,II888888888PZ;;' `8888888I8888888888888b, II888888888' 888888I8888888888888888b ,II888888888 ,888888I88888888888888888 ,d88888888888 d888888I8888888888ZZZZZZZ ,ad888888888888I 8888888I8888ZZZZZZZZZZZZZ ,d888888888888888' 888888IZZZZZZZZZZZZZZZZZZ ,d888888888888P'8P' Y888ZZZZZZZZZZZZZZZZZZZZZ ,8888888888888, " ,ZZZZZZZZZZZZZZZZZZZZZZZZ d888888888888888, ,ZZZZZZZZZZZZZZZZZZZZZZZZZZZ 888888888888888888a, _ ,ZZZZZZZZZZZZZZZZZZZZ888888888 888888888888888888888ba,_d' ,ZZZZZZZZZZZZZZZZZ88888888888888 8888888888888888888888888888bbbaaa,,,______,ZZZZZZZZZZZZZZZ888888888888888888 88888888888888888888888888888888888888888ZZZZZZZZZZZZZZZ888888888888888888888 8888888888888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888888888 888888888888888888888888888888888888888ZZZZZZZZZZZZZZ888888888888888888888888 8888888888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888888888888 88888888888888888888888888888888888ZZZZZZZZZZZZZZ8888888888888888888888888888 8888888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888 Normand 88 88888888888888888888888888888888ZZZZZZZZZZZZZZ8888888888888888888 Veilleux 88 8888888888888888888888888888888ZZZZZZZZZZZZZZ88888888888888888888888888888888 https://asciiart.website/index.php?art=art%20and%20design/mona%20lisa*/ #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef uint32_t ul; typedef int32_t l; typedef uint64_t ull; typedef int64_t ll; template <typename T> using statistics_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const l INF = 1000000000 + 9; const ll MOD = 2520; const l PRIME = 200003; const ll L_INF = 1000000000000000000LL + 7; const double EPS = 1e-5; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end() #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second ll lf, rt; ll dp[20][MOD][512][2]; //prefix | modulo | mask | smaller vector<l> num; ll calc(ll v) { if(v == 0) return 0; num.clear(); while(v) { num.PB(v%10LL); v /= 10LL; } //pos = 0 REP(2520, mod) FOR(1, 512, mask) { bool err{false}; FORE(1, 9, d) if(mask & (1 << (d-1)) && (mod % d != 0)) { err = true; break; } dp[0][mod][mask][0] = dp[0][mod][mask][1] = 1-err; } FORE(1, num.size(), pos) REP(2520, mod) REP(512, mask) REP(2, smaller) { l limit; if(smaller == 0) limit = num[pos-1]; else limit = 9; dp[pos][mod][mask][smaller] = 0; FORE(1, limit, d) { bool n_smaller{smaller}; if(d < limit) n_smaller = 1; dp[pos][mod][mask][smaller] += dp[pos-1][(mod*10+d)%MOD][mask | (1 << (d-1))][n_smaller]; } } ll res{0}; FORE(1, num.size()-1, i) res += dp[i][0][0][1]; res += dp[num.size()][0][0][0]; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> lf >> rt; cout << calc(rt) - calc(lf-1) << "\n"; } |