#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; struct splitmix64_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; template <typename K, typename V, typename Hash = splitmix64_hash> using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>; template <typename K, typename Hash = splitmix64_hash> using hash_set = hash_map<K, __gnu_pbds::null_type, Hash>; #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) #define FORD(i,a,b) for(int i = (a); i >= (b); --i) #define RI(i,n) FOR(i,1,(n)) #define REP(i,n) FOR(i,0,(n)-1) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) #define pb push_back #define st first #define nd second #define sz(w) (int) w.size() typedef vector<int> vi; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<pii, int> para; const ll inf = 1e18 + 7; const ll maxN = 1e6 + 5; const ll MOD = 504; ll dp[20][504][256][2]; ll solve(int max_len, int mod504, int mask, bool last5) { if (max_len == 0) { REP(j, 8) { if (mask & (1 << j)) { int val = j + 2; if (val == 5) { if (!last5) return 0; } else { if (mod504 % val != 0) return 0; } } } return 1; } if (dp[max_len][mod504][mask][last5] != -1) return dp[max_len][mod504][mask][last5]; ll ans = 0; RI(i, 9) { int nw_mask = mask; if (i != 1) nw_mask |= (1 << (i - 2)); ans += solve(max_len - 1, (mod504 * 10 + i) % MOD, nw_mask, i == 5); } dp[max_len][mod504][mask][last5] = ans; return ans; } bool good(ll val) { string s = to_string(val); for (auto x: s) { int z = (x - '0'); if (z == 0 || (val % z != 0)) return false; } return true; } ll getAns(ll val) { string s = to_string(val); int x = sz(s); ll ans = 0; RI(i, x - 1) { ans += solve(i, 0, 0, false); // cout << ans << endl; } int mask = 0, mod504 = 0; REP(j, x) { int val = s[j] - '0'; if (val == 0) break; RI(i, val - 1) { int nw_mask = mask; if (i > 1) nw_mask |= (1 << (i - 2)); int mod_new = (mod504 * 10 + i) % MOD; // cout << mod_new << endl; ans += solve(x - (j + 1), mod_new, nw_mask, i == 5); } mod504 = (mod504 * 10 + val) % MOD; if (val > 1) mask |= (1 << (val - 2)); } ans += good(val); return ans; } // sprawdz MODULO! int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll l, r; cin >> l >> r; int x = to_string(r).size(); REP(i, x + 1) { REP(k, 504) { REP(ll, 256) { REP(j, 2) { dp[i][k][ll][j] = -1; } } } } cout << getAns(r) - getAns(l - 1) << "\n"; 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 122 123 124 125 126 127 128 129 130 131 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; struct splitmix64_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; template <typename K, typename V, typename Hash = splitmix64_hash> using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>; template <typename K, typename Hash = splitmix64_hash> using hash_set = hash_map<K, __gnu_pbds::null_type, Hash>; #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) #define FORD(i,a,b) for(int i = (a); i >= (b); --i) #define RI(i,n) FOR(i,1,(n)) #define REP(i,n) FOR(i,0,(n)-1) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) #define pb push_back #define st first #define nd second #define sz(w) (int) w.size() typedef vector<int> vi; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<pii, int> para; const ll inf = 1e18 + 7; const ll maxN = 1e6 + 5; const ll MOD = 504; ll dp[20][504][256][2]; ll solve(int max_len, int mod504, int mask, bool last5) { if (max_len == 0) { REP(j, 8) { if (mask & (1 << j)) { int val = j + 2; if (val == 5) { if (!last5) return 0; } else { if (mod504 % val != 0) return 0; } } } return 1; } if (dp[max_len][mod504][mask][last5] != -1) return dp[max_len][mod504][mask][last5]; ll ans = 0; RI(i, 9) { int nw_mask = mask; if (i != 1) nw_mask |= (1 << (i - 2)); ans += solve(max_len - 1, (mod504 * 10 + i) % MOD, nw_mask, i == 5); } dp[max_len][mod504][mask][last5] = ans; return ans; } bool good(ll val) { string s = to_string(val); for (auto x: s) { int z = (x - '0'); if (z == 0 || (val % z != 0)) return false; } return true; } ll getAns(ll val) { string s = to_string(val); int x = sz(s); ll ans = 0; RI(i, x - 1) { ans += solve(i, 0, 0, false); // cout << ans << endl; } int mask = 0, mod504 = 0; REP(j, x) { int val = s[j] - '0'; if (val == 0) break; RI(i, val - 1) { int nw_mask = mask; if (i > 1) nw_mask |= (1 << (i - 2)); int mod_new = (mod504 * 10 + i) % MOD; // cout << mod_new << endl; ans += solve(x - (j + 1), mod_new, nw_mask, i == 5); } mod504 = (mod504 * 10 + val) % MOD; if (val > 1) mask |= (1 << (val - 2)); } ans += good(val); return ans; } // sprawdz MODULO! int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll l, r; cin >> l >> r; int x = to_string(r).size(); REP(i, x + 1) { REP(k, 504) { REP(ll, 256) { REP(j, 2) { dp[i][k][ll][j] = -1; } } } } cout << getAns(r) - getAns(l - 1) << "\n"; return 0; } |