#include <bits/stdc++.h> #define int long long using namespace std; const int LCM = 2520; int dp[19][2][LCM][(1 << 9)]; vector < int > q; int rec(int pos, bool flg, int rem, int mask){ //cout << pos << " " << flg << " " << rem << " " << mask << "\n"; int& ans = dp[pos][flg][rem][mask]; if(ans != -1) return ans; ans = 0; if(pos == (int)q.size()){ ans = 1; for (int d = 1; d < 10; d++){ if((mask >> (d - 1)) & 1){ if(rem % d != 0){ ans = 0; } } } } else{ for(int d = 0; d < 10; d++){ if(flg && d > q[pos]) continue; if (mask && d == 0) continue; bool new_flg = (flg == 1 ? d == q[pos] : 0); int new_rem = (rem * 10 + d) % LCM; int new_mask = mask; if (d != 0) new_mask |= (1 << (d - 1)); ans += rec(pos + 1, new_flg, new_rem, new_mask); } } return ans; } int f(int n){ q.clear(); do{ q.push_back(n % 10); n /= 10; }while(n > 0); reverse(q.begin(), q.end()); memset(dp, -1, sizeof(dp)); return rec(0, 1, 0, 0); } main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int l, r; cin >> l >> r; cout << f(r) - f(l - 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 | #include <bits/stdc++.h> #define int long long using namespace std; const int LCM = 2520; int dp[19][2][LCM][(1 << 9)]; vector < int > q; int rec(int pos, bool flg, int rem, int mask){ //cout << pos << " " << flg << " " << rem << " " << mask << "\n"; int& ans = dp[pos][flg][rem][mask]; if(ans != -1) return ans; ans = 0; if(pos == (int)q.size()){ ans = 1; for (int d = 1; d < 10; d++){ if((mask >> (d - 1)) & 1){ if(rem % d != 0){ ans = 0; } } } } else{ for(int d = 0; d < 10; d++){ if(flg && d > q[pos]) continue; if (mask && d == 0) continue; bool new_flg = (flg == 1 ? d == q[pos] : 0); int new_rem = (rem * 10 + d) % LCM; int new_mask = mask; if (d != 0) new_mask |= (1 << (d - 1)); ans += rec(pos + 1, new_flg, new_rem, new_mask); } } return ans; } int f(int n){ q.clear(); do{ q.push_back(n % 10); n /= 10; }while(n > 0); reverse(q.begin(), q.end()); memset(dp, -1, sizeof(dp)); return rec(0, 1, 0, 0); } main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int l, r; cin >> l >> r; cout << f(r) - f(l - 1) << "\n"; } |