#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } const int K = 2520; long long dp[21][2][1<<9][K]; long long ways(long long n) { if(n == 0) { return 0; } string s = to_string(n); memset(dp, 0, sizeof(dp)); for(int pos = 0; pos < (int) s.length(); ++pos) { dp[pos][pos>0][0][0] = 1; } for(int pos = 0; pos < (int) s.length(); ++pos) { for(int is_smaller : {0, 1}) { for(int mask = 0; mask < (1 << 9); ++mask) { for(int rem = 0; rem < K; ++rem) { long long me = dp[pos][is_smaller][mask][rem]; if(me == 0) { continue; } for(int digit = 1; digit < 10; ++digit) { if(digit > s[pos]-'0' && !is_smaller) { continue; } dp[pos+1][is_smaller || digit < s[pos]-'0'][mask|(1<<(digit-1))][(rem*10+digit)%K] += me; } } } } } long long answer = 0; for(int is_smaller : {0, 1}) { for(int mask = 0; mask < (1 << 9); ++mask) { int lcm = 1; for(int dig = 1; dig < 10; ++dig) { if(mask & (1 << (dig - 1))) { lcm = lcm * dig / __gcd(lcm, dig); } } for(int rem = 0; rem < K; rem += lcm) { answer += dp[s.length()][is_smaller][mask][rem]; } } } return answer; } int main() { long long l, r; scanf("%lld%lld", &l, &r); printf("%lld\n", ways(r) - ways(l-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 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } const int K = 2520; long long dp[21][2][1<<9][K]; long long ways(long long n) { if(n == 0) { return 0; } string s = to_string(n); memset(dp, 0, sizeof(dp)); for(int pos = 0; pos < (int) s.length(); ++pos) { dp[pos][pos>0][0][0] = 1; } for(int pos = 0; pos < (int) s.length(); ++pos) { for(int is_smaller : {0, 1}) { for(int mask = 0; mask < (1 << 9); ++mask) { for(int rem = 0; rem < K; ++rem) { long long me = dp[pos][is_smaller][mask][rem]; if(me == 0) { continue; } for(int digit = 1; digit < 10; ++digit) { if(digit > s[pos]-'0' && !is_smaller) { continue; } dp[pos+1][is_smaller || digit < s[pos]-'0'][mask|(1<<(digit-1))][(rem*10+digit)%K] += me; } } } } } long long answer = 0; for(int is_smaller : {0, 1}) { for(int mask = 0; mask < (1 << 9); ++mask) { int lcm = 1; for(int dig = 1; dig < 10; ++dig) { if(mask & (1 << (dig - 1))) { lcm = lcm * dig / __gcd(lcm, dig); } } for(int rem = 0; rem < K; rem += lcm) { answer += dp[s.length()][is_smaller][mask][rem]; } } } return answer; } int main() { long long l, r; scanf("%lld%lld", &l, &r); printf("%lld\n", ways(r) - ways(l-1)); } |