#include <iostream> #include <string> #include <cstring> using namespace std; int vis[1024]; const int MOD = 9 * 8 * 7 * 5; long long dp[20][2][2][MOD][50]; int len, len2; string l, r; long long pot10[21]; long long calc(int idx, bool low, bool high, int rem, int mask) { if (idx == len) { return rem == 0; } if (dp[idx][low][high][rem][vis[mask]] != -1) { return dp[idx][low][high][rem][vis[mask]]; } int low1 = 1; int high1 = 9; int x = len - len2; if (low) { low1 = l[idx] - '0'; } if (high) { high1 = r[idx] - '0'; } //if (idx == x && low1 == 0) low1=1; long long ret = 0; if (low1 == high1 && low1 == 0) { } //else for (int c = low1; c <= high1; c++) { //if (c == 0 && idx < x) continue; if (c < 2 || ((mask >> (c - 2)) & 1)) { int nrem = (rem - pot10[len - idx - 1] * c % MOD + MOD) % MOD; ret += calc(idx + 1, low && low1 == c, high && high1 == c, nrem, mask); } } dp[idx][low][high][rem][vis[mask]] = ret; return ret; } int main() { vis[0] = 1; int v = 2; pot10[0] = 1; for (int i = 1; i <= 20; i++) { pot10[i] = pot10[i - 1] * 10 % MOD; } for (int i = 0; i < MOD; i++) { int mask = 0; for (int j = 2; j < 10; j++) { if (i % j == 0) { mask |= 1 << (j - 2); } } if (!vis[mask]) { vis[mask] = v; v++; } } //printf("v = %d\n", v); cin >> l >> r; len2 = l.size(); string z(r.size() - l.size(), '0'); l = z + l; for (int i = z.size(); i < l.size(); i++) { if (l[i] == '0') { for (int j = i; j < l.size(); j++) { l[j] = '1'; } } } len = r.size(); //cout << l << " " << r << endl; memset(dp, -1, sizeof(dp)); long long ret = 0; for (int i = 0; i < MOD; i++) { int mask = 0; for (int j = 2; j < 10; j++) { if (i % j == 0) { mask |= 1 << (j - 2); } } ret += calc(0, true, true, i, mask); } //printf("%lld\n", ret); cout << ret; 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 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 141 142 | #include <iostream> #include <string> #include <cstring> using namespace std; int vis[1024]; const int MOD = 9 * 8 * 7 * 5; long long dp[20][2][2][MOD][50]; int len, len2; string l, r; long long pot10[21]; long long calc(int idx, bool low, bool high, int rem, int mask) { if (idx == len) { return rem == 0; } if (dp[idx][low][high][rem][vis[mask]] != -1) { return dp[idx][low][high][rem][vis[mask]]; } int low1 = 1; int high1 = 9; int x = len - len2; if (low) { low1 = l[idx] - '0'; } if (high) { high1 = r[idx] - '0'; } //if (idx == x && low1 == 0) low1=1; long long ret = 0; if (low1 == high1 && low1 == 0) { } //else for (int c = low1; c <= high1; c++) { //if (c == 0 && idx < x) continue; if (c < 2 || ((mask >> (c - 2)) & 1)) { int nrem = (rem - pot10[len - idx - 1] * c % MOD + MOD) % MOD; ret += calc(idx + 1, low && low1 == c, high && high1 == c, nrem, mask); } } dp[idx][low][high][rem][vis[mask]] = ret; return ret; } int main() { vis[0] = 1; int v = 2; pot10[0] = 1; for (int i = 1; i <= 20; i++) { pot10[i] = pot10[i - 1] * 10 % MOD; } for (int i = 0; i < MOD; i++) { int mask = 0; for (int j = 2; j < 10; j++) { if (i % j == 0) { mask |= 1 << (j - 2); } } if (!vis[mask]) { vis[mask] = v; v++; } } //printf("v = %d\n", v); cin >> l >> r; len2 = l.size(); string z(r.size() - l.size(), '0'); l = z + l; for (int i = z.size(); i < l.size(); i++) { if (l[i] == '0') { for (int j = i; j < l.size(); j++) { l[j] = '1'; } } } len = r.size(); //cout << l << " " << r << endl; memset(dp, -1, sizeof(dp)); long long ret = 0; for (int i = 0; i < MOD; i++) { int mask = 0; for (int j = 2; j < 10; j++) { if (i % j == 0) { mask |= 1 << (j - 2); } } ret += calc(0, true, true, i, mask); } //printf("%lld\n", ret); cout << ret; return 0; } |