#include <iostream> #include <vector> int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } int lcm(int a, int b) { return a*b/gcd(a, b); } int64_t K[20][1000][512]; int64_t G[20][1000][512]; int M[20]; int D[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int D2[10]; int64_t sub_calc(const std::vector<int>& D, const std::string& target) { int ll = 1; for (int l : D) ll = lcm(ll, l); if (ll % 10 == 0) return 0; int bits = (1<<D.size())-1; for (int i=0;i<ll;++i) { for (int j=0;j<target.size();++j) { for (int k=0;k<=bits;++k) { K[j][i][k] = G[j][i][k] = 0; } } } for (int i=0;i<D.size();++i) D2[i] = D[i]%ll; for (int i=0;i<D.size();++i) K[0][D2[i]][1<<i] += 1; int mult = 1; M[0] = mult; int64_t sum = 0; for (int i=1;i<target.size();++i) { sum += K[i-1][0][bits]; mult = mult*10%ll; M[i] = mult; for (int li=0;li<D.size();++li) { int dd = D2[li] * mult%ll; int bb = 1<<li; for (int j=0;j<ll;++j) { int mod = (j+dd)%ll; for (int b=0;b<=bits;++b) { K[i][mod][b|bb] += K[i-1][j][b]; } } } } int front_bits = 0; int front_sum = 0; bool early = false; for (int i=target.size()-1;i>0;--i) { int li = 0; for (;li<D.size() && D[li]<target[target.size()-1-i]-'0';++li) { int dd = D2[li] * M[i]%ll; int bb = 1<<li; for (int j=0;j<ll;++j) { int mod = (j+dd+front_sum)%ll; for (int b=0;b<=bits;++b) { G[i][mod][b|bb|front_bits] += K[i-1][j][b]; } } } sum += G[i][0][bits]; if (li < D.size() && D[li] == target[target.size()-1-i]-'0') { // keep going front_bits |= 1<<li; front_sum = (front_sum+D2[li]*M[i])%ll; } else { early = true; break; } } if (!early){ int li = 0; for (;li<D.size() && D[li]<=target.back()-'0';++li) { int dd = D2[li] * M[0]%ll; int bb = 1<<li; if ((dd+front_sum)%ll == 0 && (bb|front_bits) == bits) ++sum; } } return sum; } int64_t calc(int64_t target) { if (target == 0) return 0; auto target_str = std::to_string(target); int64_t sum = 0; for (int i=0;i<512;++i) { std::vector<int> D; for (int j=0;j<9;++j) { if (i & (1<<j)) D.push_back(j+1); } int64_t c = sub_calc(D, target_str); sum += c; } return sum; } int main() { std::ios_base::sync_with_stdio(0); int64_t l,r; std::cin >> l >> r; std::cout << calc(r) - calc(l-1) << std::endl; 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 | #include <iostream> #include <vector> int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } int lcm(int a, int b) { return a*b/gcd(a, b); } int64_t K[20][1000][512]; int64_t G[20][1000][512]; int M[20]; int D[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int D2[10]; int64_t sub_calc(const std::vector<int>& D, const std::string& target) { int ll = 1; for (int l : D) ll = lcm(ll, l); if (ll % 10 == 0) return 0; int bits = (1<<D.size())-1; for (int i=0;i<ll;++i) { for (int j=0;j<target.size();++j) { for (int k=0;k<=bits;++k) { K[j][i][k] = G[j][i][k] = 0; } } } for (int i=0;i<D.size();++i) D2[i] = D[i]%ll; for (int i=0;i<D.size();++i) K[0][D2[i]][1<<i] += 1; int mult = 1; M[0] = mult; int64_t sum = 0; for (int i=1;i<target.size();++i) { sum += K[i-1][0][bits]; mult = mult*10%ll; M[i] = mult; for (int li=0;li<D.size();++li) { int dd = D2[li] * mult%ll; int bb = 1<<li; for (int j=0;j<ll;++j) { int mod = (j+dd)%ll; for (int b=0;b<=bits;++b) { K[i][mod][b|bb] += K[i-1][j][b]; } } } } int front_bits = 0; int front_sum = 0; bool early = false; for (int i=target.size()-1;i>0;--i) { int li = 0; for (;li<D.size() && D[li]<target[target.size()-1-i]-'0';++li) { int dd = D2[li] * M[i]%ll; int bb = 1<<li; for (int j=0;j<ll;++j) { int mod = (j+dd+front_sum)%ll; for (int b=0;b<=bits;++b) { G[i][mod][b|bb|front_bits] += K[i-1][j][b]; } } } sum += G[i][0][bits]; if (li < D.size() && D[li] == target[target.size()-1-i]-'0') { // keep going front_bits |= 1<<li; front_sum = (front_sum+D2[li]*M[i])%ll; } else { early = true; break; } } if (!early){ int li = 0; for (;li<D.size() && D[li]<=target.back()-'0';++li) { int dd = D2[li] * M[0]%ll; int bb = 1<<li; if ((dd+front_sum)%ll == 0 && (bb|front_bits) == bits) ++sum; } } return sum; } int64_t calc(int64_t target) { if (target == 0) return 0; auto target_str = std::to_string(target); int64_t sum = 0; for (int i=0;i<512;++i) { std::vector<int> D; for (int j=0;j<9;++j) { if (i & (1<<j)) D.push_back(j+1); } int64_t c = sub_calc(D, target_str); sum += c; } return sum; } int main() { std::ios_base::sync_with_stdio(0); int64_t l,r; std::cin >> l >> r; std::cout << calc(r) - calc(l-1) << std::endl; return 0; } |