#include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const ll INF = 1e18; const int br = 2520; ll dp[(1 << 8)][br][19]; vi valid[br]; ll f(ll x) { string s = to_string(x); int cur = 0; int a = 1; ll res = 0; ll pot = 1; while(pot < INF && pot * 10 <= x) pot *= 10; //dodaj do wyniku, te sekwencje z zerami na początku for(int l = 0; l < (int)s.size(); ++l) { for(int r = 0; r < br; ++r) { for(int mask : valid[r]) { res += dp[mask][r][l]; } } } for(int i = 0; i < (int)s.size(); ++i) { cur *= 10; for(int d = 1; d < s[i] - '0'; ++d) { cur += d; int a2 = a; if(d > 1) a2 = a2 * d / __gcd(a2, d); //~ cout << " " << a2 << "\n"; for(int r = 0; r < br; r++) { // cur * pot + r2 = r if(r % a2 != 0) continue; int r2 = (r - cur * (pot % br))%br; if(r2 < 0) r2 += br; for(int mask : valid[r]) { //~ if(dp[mask][r2][(int)s.size() - i - 1] > 0) { //~ cout << mask << " " << r2 << "\n"; //~ } res += dp[mask][r2][(int)s.size() - i - 1]; } } cur -= d; } pot /= 10; if(s[i] == '0') break; cur += (s[i] - '0'); cur %= (5 * 7 * 8 * 9); a = a * (s[i] - '0') / __gcd(a, s[i] - '0'); //~ cout << res << "\n"; } return res; } ll l, rr; int main() { for(int i = 0; i < br; ++i) { for(int mask = 0; mask < (1 << 8); ++mask) { bool ok = 1; for(int j = 0; j < 8; ++j) { if(mask & (1 << j)) { if(i % (j + 2) != 0) ok = 0; } } if(ok) valid[i].PB(mask); } } dp[0][0][0] = 1; for(int i = 0; i <= 17; ++i) { for(int r = 0; r < br; ++r) { for(int mask = 0; mask < (1 << 8); ++mask) { for(int d = 1; d <= 9; ++d) { int new_mask = mask; if(d > 1) new_mask |= (1 << (d - 2)); dp[new_mask][(r * 10 + d)%(br)][i + 1] += dp[mask][r][i]; } //~ dp[mask][r][i + 1] += dp[mask][r][i]; } } } cin >> l >> rr; cout << f(rr + 1) - f(l); //~ for(int i = 1; i <= 10*1000; ++i) { //~ cout << i << " " << f(i + 1) - 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 | #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const ll INF = 1e18; const int br = 2520; ll dp[(1 << 8)][br][19]; vi valid[br]; ll f(ll x) { string s = to_string(x); int cur = 0; int a = 1; ll res = 0; ll pot = 1; while(pot < INF && pot * 10 <= x) pot *= 10; //dodaj do wyniku, te sekwencje z zerami na początku for(int l = 0; l < (int)s.size(); ++l) { for(int r = 0; r < br; ++r) { for(int mask : valid[r]) { res += dp[mask][r][l]; } } } for(int i = 0; i < (int)s.size(); ++i) { cur *= 10; for(int d = 1; d < s[i] - '0'; ++d) { cur += d; int a2 = a; if(d > 1) a2 = a2 * d / __gcd(a2, d); //~ cout << " " << a2 << "\n"; for(int r = 0; r < br; r++) { // cur * pot + r2 = r if(r % a2 != 0) continue; int r2 = (r - cur * (pot % br))%br; if(r2 < 0) r2 += br; for(int mask : valid[r]) { //~ if(dp[mask][r2][(int)s.size() - i - 1] > 0) { //~ cout << mask << " " << r2 << "\n"; //~ } res += dp[mask][r2][(int)s.size() - i - 1]; } } cur -= d; } pot /= 10; if(s[i] == '0') break; cur += (s[i] - '0'); cur %= (5 * 7 * 8 * 9); a = a * (s[i] - '0') / __gcd(a, s[i] - '0'); //~ cout << res << "\n"; } return res; } ll l, rr; int main() { for(int i = 0; i < br; ++i) { for(int mask = 0; mask < (1 << 8); ++mask) { bool ok = 1; for(int j = 0; j < 8; ++j) { if(mask & (1 << j)) { if(i % (j + 2) != 0) ok = 0; } } if(ok) valid[i].PB(mask); } } dp[0][0][0] = 1; for(int i = 0; i <= 17; ++i) { for(int r = 0; r < br; ++r) { for(int mask = 0; mask < (1 << 8); ++mask) { for(int d = 1; d <= 9; ++d) { int new_mask = mask; if(d > 1) new_mask |= (1 << (d - 2)); dp[new_mask][(r * 10 + d)%(br)][i + 1] += dp[mask][r][i]; } //~ dp[mask][r][i + 1] += dp[mask][r][i]; } } } cin >> l >> rr; cout << f(rr + 1) - f(l); //~ for(int i = 1; i <= 10*1000; ++i) { //~ cout << i << " " << f(i + 1) - 1 << "\n"; //~ } } |