#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) ((int)a.size()) template<class T> ostream& operator<<(ostream &stream, vector<T> &v) { stream << "["; for(auto elem : v) { stream << elem << ", "; } stream << "]"; return stream; } const int mod = 5*7*8*9; ll dp[19][512][mod]; bool is_valid(int mask, int hash) { for(int d=1; d <= 9; d++) { if((mask&(1<<(d-1))) == 0) { continue; } if(hash%d != 0) { return false; } } return true; } int lcms[512]; int gcd(int a, int b) { while(b) { int c = a%b; a=b; b=c; } return a; } void precompute_lcms() { for(int mask=0; mask < 512; mask++) { int l = 1; for(int d=1; d <= 9; d++) { if(mask&(1<<(d-1))) { l = l*d/gcd(l, d); } } lcms[mask] = l; } } void fill_dp(int maxd) { dp[0][0][0] = 1; ll pt = 1; for(int c=0; c < maxd; c++) { for(int mask=0; mask < 512; mask++) { for(int hash=0; hash < mod; hash++) { for(int d=1; d <= 9; d++) { dp[c+1][mask|(1<<(d-1))][(hash+d*pt)%mod]+=dp[c][mask][hash]; } } } pt*=10; } // for(int c=0; c <= maxd; c++) { // for(int mask=0; mask < 32; mask++) { // for(int hash=0; hash < mod; hash++) { // if(dp[c][mask][hash]) // cout << c << " " << mask << " " << hash << " " << dp[c][mask][hash] << "\n"; // } // } // cout << "\n"; // } } ll pot(ll a) { a++; ll res = 0; ll b = a; vector<int> digits; ll pt =1; while(b) { digits.PB(b%10); b/=10; pt*=10; } // cout << digits << "\n"; int prefix_hash = 0; int prefix_mask = 0; bool prefix_valid = true; for(int c=SIZE(digits)-1; c >= 0; c--) { pt/=10; if(prefix_valid) { for(int d=1; d < digits[c]; d++) { for(int mask=0; mask<512; mask++) { int effective_mask = (mask|prefix_mask|(1<<(d-1))); int l = lcms[effective_mask]; for(int hash=((-prefix_hash-d*pt)%l+l)%l; hash < mod; hash+=l) { res+=dp[c][mask][hash]; } } } } // cout << res << "\n"; for(int mask=0; mask<512; mask++) { for(int hash=0; hash<mod; hash++) { if(is_valid(mask, hash)) { res+=dp[c][mask][hash]; } } } // cout << res << "\n"; prefix_hash = (prefix_hash+digits[c]*pt)%mod; prefix_mask|= (1<<(digits[c]-1)); if(digits[c] == 0) { prefix_valid = false; } } return res; } int main() { ll le, ri; scanf("%lld%lld", &le, &ri); precompute_lcms(); fill_dp(18); printf("%lld\n", pot(ri)-pot(le-1)); }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) ((int)a.size()) template<class T> ostream& operator<<(ostream &stream, vector<T> &v) { stream << "["; for(auto elem : v) { stream << elem << ", "; } stream << "]"; return stream; } const int mod = 5*7*8*9; ll dp[19][512][mod]; bool is_valid(int mask, int hash) { for(int d=1; d <= 9; d++) { if((mask&(1<<(d-1))) == 0) { continue; } if(hash%d != 0) { return false; } } return true; } int lcms[512]; int gcd(int a, int b) { while(b) { int c = a%b; a=b; b=c; } return a; } void precompute_lcms() { for(int mask=0; mask < 512; mask++) { int l = 1; for(int d=1; d <= 9; d++) { if(mask&(1<<(d-1))) { l = l*d/gcd(l, d); } } lcms[mask] = l; } } void fill_dp(int maxd) { dp[0][0][0] = 1; ll pt = 1; for(int c=0; c < maxd; c++) { for(int mask=0; mask < 512; mask++) { for(int hash=0; hash < mod; hash++) { for(int d=1; d <= 9; d++) { dp[c+1][mask|(1<<(d-1))][(hash+d*pt)%mod]+=dp[c][mask][hash]; } } } pt*=10; } // for(int c=0; c <= maxd; c++) { // for(int mask=0; mask < 32; mask++) { // for(int hash=0; hash < mod; hash++) { // if(dp[c][mask][hash]) // cout << c << " " << mask << " " << hash << " " << dp[c][mask][hash] << "\n"; // } // } // cout << "\n"; // } } ll pot(ll a) { a++; ll res = 0; ll b = a; vector<int> digits; ll pt =1; while(b) { digits.PB(b%10); b/=10; pt*=10; } // cout << digits << "\n"; int prefix_hash = 0; int prefix_mask = 0; bool prefix_valid = true; for(int c=SIZE(digits)-1; c >= 0; c--) { pt/=10; if(prefix_valid) { for(int d=1; d < digits[c]; d++) { for(int mask=0; mask<512; mask++) { int effective_mask = (mask|prefix_mask|(1<<(d-1))); int l = lcms[effective_mask]; for(int hash=((-prefix_hash-d*pt)%l+l)%l; hash < mod; hash+=l) { res+=dp[c][mask][hash]; } } } } // cout << res << "\n"; for(int mask=0; mask<512; mask++) { for(int hash=0; hash<mod; hash++) { if(is_valid(mask, hash)) { res+=dp[c][mask][hash]; } } } // cout << res << "\n"; prefix_hash = (prefix_hash+digits[c]*pt)%mod; prefix_mask|= (1<<(digits[c]-1)); if(digits[c] == 0) { prefix_valid = false; } } return res; } int main() { ll le, ri; scanf("%lld%lld", &le, &ri); precompute_lcms(); fill_dp(18); printf("%lld\n", pot(ri)-pot(le-1)); } |