#include <cstdio> #include <vector> using namespace std; const int Q = 2520; long long dp[512][Q]; long long e[512][Q]; long long licz(long long a) { vector<int> d; while (a) { d.push_back(a % 10); a /= 10; } for (int m = 0; m < 512; m++) for(int x = 0; x < Q; x++) dp[m][x] = 0; int ma = 0, r = 0, w = 1; for (int i = d.size()-1; i >= 0; i--) { for (int m = 0; m < 512; m++) for(int x = 0; x < Q; x++) { e[m][x] = dp[m][x]; dp[m][x] = 0; } dp[0][0] = 1; for (int c = 1; c < d[i]; c++) { dp[ma | (1 << (c-1))][(10 * r + c) % Q] += w; } if(d[i]) ma |= 1<<(d[i]-1); else w = 0; r = (10 * r + d[i]) % Q; for (int m = 0; m < 512; m++) for (int x = 0; x < Q; x++) if (e[m][x]) { for (int c = 1; c <= 9; c++) dp[m | (1 << (c-1))][(10 * x + c) % Q] += e[m][x]; } } long long ret = 0; for (int m = 1; m < 512; m++) for(int x = 0; x < Q; x++) { bool ok = true; for (int c = 2; c<=9; c++) if (m & (1 << (c-1))) { if (x % c) ok = false; } if (ok) ret += dp[m][x]; } return ret; } int main() { long long a,b; scanf("%lld %lld",&a,&b); printf("%lld\n", licz(b+1) - licz(a)); 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 | #include <cstdio> #include <vector> using namespace std; const int Q = 2520; long long dp[512][Q]; long long e[512][Q]; long long licz(long long a) { vector<int> d; while (a) { d.push_back(a % 10); a /= 10; } for (int m = 0; m < 512; m++) for(int x = 0; x < Q; x++) dp[m][x] = 0; int ma = 0, r = 0, w = 1; for (int i = d.size()-1; i >= 0; i--) { for (int m = 0; m < 512; m++) for(int x = 0; x < Q; x++) { e[m][x] = dp[m][x]; dp[m][x] = 0; } dp[0][0] = 1; for (int c = 1; c < d[i]; c++) { dp[ma | (1 << (c-1))][(10 * r + c) % Q] += w; } if(d[i]) ma |= 1<<(d[i]-1); else w = 0; r = (10 * r + d[i]) % Q; for (int m = 0; m < 512; m++) for (int x = 0; x < Q; x++) if (e[m][x]) { for (int c = 1; c <= 9; c++) dp[m | (1 << (c-1))][(10 * x + c) % Q] += e[m][x]; } } long long ret = 0; for (int m = 1; m < 512; m++) for(int x = 0; x < Q; x++) { bool ok = true; for (int c = 2; c<=9; c++) if (m & (1 << (c-1))) { if (x % c) ok = false; } if (ok) ret += dp[m][x]; } return ret; } int main() { long long a,b; scanf("%lld %lld",&a,&b); printf("%lld\n", licz(b+1) - licz(a)); return 0; } |