#include <bits/stdc++.h> using namespace std; const long long inf = 1e18 + 5; const int MOD = 2520; //int mask[20]; //int mod[20]; long long dp[20][256][2520]; int ile_cyfr(long long x) { int ile = 0; while (x) { x /= 10; ile++; } return ile; } void get_cyfry(long long x, vector<int> &cyfry) { int ile = ile_cyfr(x); cyfry.resize(ile); for (int i = 0; i < ile; i++) { cyfry[i] = x % 10; x /= 10; } } long long nozero(long long x) { int ile = ile_cyfr(x); vector<int> cyfry; get_cyfry(x, cyfry); bool zero = false; int ind = 0; for (int i = 0; i < ile; i++) { if (cyfry[i] == 0) { zero = true; ind = i + 1; } else if (cyfry[i] == 1 && zero) { ind = i + 1; } else { zero = false; } } if (ind == ile) { int res = 0; for (int i = 1; i < ile; i++) { res = res * 10 + 9; } return res; } if (ind > 0) { cyfry[ind]--; } for (int i = 0; i < ind; i++) { cyfry[i] = 9; } long long res = 0; for (int i = ile - 1; i >= 0; i--) { res = res * 10 + cyfry[i]; } return res; } void clear() { for (int i = 0; i < 20; i++) { for (int j = 0; j < 256; j++) { for (int k = 0; k < 2520; k++) { dp[i][j][k] = 0; } } } } int iloc(int mask) { int ile = 1; bool czydwa = false; if (mask & (1<<6)) { czydwa = true; ile *= 8; } else if (mask & (1<<2)) { czydwa = true; ile *= 4; } else if (mask & (1<<0)) { czydwa = true; ile *= 2; } if (mask & (1<<3)) { ile *= 5; } if (mask & (1<<5)) { ile *= 7; } bool czytrzy = false; if (mask & (1<<7)) { ile *= 9; czytrzy = true; } else if (mask & (1<<1)) { ile *= 3; czytrzy = true; } if (mask & (1<<4)) { if (!czytrzy) { ile *= 3; } if (!czydwa) { ile *= 2; } } return ile; } int distinct(long long x) { vector<int> cyfry; get_cyfry(x, cyfry); for (int i : cyfry) { if (x % i != 0) { return 0; } } return 1; } long long cyfrowe(int ile) { if (ile == 0) return 0; long long res = 0; clear(); dp[0][0][0] = 1; for (int i = 1; i <= ile; i++) { for (int j = 0; j < 256; j++) { for (int k = 0; k < 2520; k++) { dp[i][j][(k * 10 + 1) % 2520] += dp[i - 1][j][k]; for (int l = 2; l < 10; l++) { dp[i][j | (1 << (l-2))][(k * 10 + l) % 2520] += dp[i - 1][j][k]; } } } } for (int i = 1; i <= ile; i++) { for (int j = 0; j < 256; j++) { int mnoz = iloc(j); for (int k = 0; k < 2520; k += mnoz) { res += dp[i][j][k]; } } } return res; } void info(int i, int j, int k) { if (dp[i][j][k] == 0) return; cout << "(" << i << ", " << j << ", " << k << ") -> " << dp[i][j][k] << endl; } long long smaller(long long x) { if (x == 0) { return 0; } x = nozero(x); int mask = 0; int mod = 0; vector<int> cyfry; int ile = ile_cyfr(x); get_cyfry(x, cyfry); long long res = 0; clear(); for (int i = 1; i <= ile; i++) { for (int j = 0; j < 256; j++) { for (int k = 0; k < 2520; k++) { dp[i][j][(k * 10 + 1) % 2520] += dp[i - 1][j][k]; for (int l = 2; l < 10; l++) { dp[i][j | (1 << (l-2))][(k * 10 + l) % 2520] += dp[i - 1][j][k]; } } } if (cyfry[ile - i] > 1) { dp[i][mask][(mod * 10 + 1) % 2520]++; } for (int j = 2; j < cyfry[ile - i]; j++) { dp[i][mask | (1 << (j-2))][(mod * 10 + j) % 2520]++; } int wart = 0; if (cyfry[ile - i] > 1) { wart = (1 << (cyfry[ile - i] - 2)); } mask = (mask | wart); mod = (10 * mod + cyfry[ile - i]) % 2520; } for (int i = 0; i < 256; i++) { int mnoz = iloc(i); for (int j = 0; j < 2520; j += mnoz) { // info(ile, i, j); res += dp[ile][i][j]; } } long long res2 = cyfrowe(ile - 1); // cout << "resik " << res2 << endl; return res + distinct(x) + res2; } int main() { long long l, r; scanf("%lld%lld", &l, &r); long long big = smaller(r); long long small = smaller(l - 1); long long res = big - small; printf("%lld\n", res); }
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | #include <bits/stdc++.h> using namespace std; const long long inf = 1e18 + 5; const int MOD = 2520; //int mask[20]; //int mod[20]; long long dp[20][256][2520]; int ile_cyfr(long long x) { int ile = 0; while (x) { x /= 10; ile++; } return ile; } void get_cyfry(long long x, vector<int> &cyfry) { int ile = ile_cyfr(x); cyfry.resize(ile); for (int i = 0; i < ile; i++) { cyfry[i] = x % 10; x /= 10; } } long long nozero(long long x) { int ile = ile_cyfr(x); vector<int> cyfry; get_cyfry(x, cyfry); bool zero = false; int ind = 0; for (int i = 0; i < ile; i++) { if (cyfry[i] == 0) { zero = true; ind = i + 1; } else if (cyfry[i] == 1 && zero) { ind = i + 1; } else { zero = false; } } if (ind == ile) { int res = 0; for (int i = 1; i < ile; i++) { res = res * 10 + 9; } return res; } if (ind > 0) { cyfry[ind]--; } for (int i = 0; i < ind; i++) { cyfry[i] = 9; } long long res = 0; for (int i = ile - 1; i >= 0; i--) { res = res * 10 + cyfry[i]; } return res; } void clear() { for (int i = 0; i < 20; i++) { for (int j = 0; j < 256; j++) { for (int k = 0; k < 2520; k++) { dp[i][j][k] = 0; } } } } int iloc(int mask) { int ile = 1; bool czydwa = false; if (mask & (1<<6)) { czydwa = true; ile *= 8; } else if (mask & (1<<2)) { czydwa = true; ile *= 4; } else if (mask & (1<<0)) { czydwa = true; ile *= 2; } if (mask & (1<<3)) { ile *= 5; } if (mask & (1<<5)) { ile *= 7; } bool czytrzy = false; if (mask & (1<<7)) { ile *= 9; czytrzy = true; } else if (mask & (1<<1)) { ile *= 3; czytrzy = true; } if (mask & (1<<4)) { if (!czytrzy) { ile *= 3; } if (!czydwa) { ile *= 2; } } return ile; } int distinct(long long x) { vector<int> cyfry; get_cyfry(x, cyfry); for (int i : cyfry) { if (x % i != 0) { return 0; } } return 1; } long long cyfrowe(int ile) { if (ile == 0) return 0; long long res = 0; clear(); dp[0][0][0] = 1; for (int i = 1; i <= ile; i++) { for (int j = 0; j < 256; j++) { for (int k = 0; k < 2520; k++) { dp[i][j][(k * 10 + 1) % 2520] += dp[i - 1][j][k]; for (int l = 2; l < 10; l++) { dp[i][j | (1 << (l-2))][(k * 10 + l) % 2520] += dp[i - 1][j][k]; } } } } for (int i = 1; i <= ile; i++) { for (int j = 0; j < 256; j++) { int mnoz = iloc(j); for (int k = 0; k < 2520; k += mnoz) { res += dp[i][j][k]; } } } return res; } void info(int i, int j, int k) { if (dp[i][j][k] == 0) return; cout << "(" << i << ", " << j << ", " << k << ") -> " << dp[i][j][k] << endl; } long long smaller(long long x) { if (x == 0) { return 0; } x = nozero(x); int mask = 0; int mod = 0; vector<int> cyfry; int ile = ile_cyfr(x); get_cyfry(x, cyfry); long long res = 0; clear(); for (int i = 1; i <= ile; i++) { for (int j = 0; j < 256; j++) { for (int k = 0; k < 2520; k++) { dp[i][j][(k * 10 + 1) % 2520] += dp[i - 1][j][k]; for (int l = 2; l < 10; l++) { dp[i][j | (1 << (l-2))][(k * 10 + l) % 2520] += dp[i - 1][j][k]; } } } if (cyfry[ile - i] > 1) { dp[i][mask][(mod * 10 + 1) % 2520]++; } for (int j = 2; j < cyfry[ile - i]; j++) { dp[i][mask | (1 << (j-2))][(mod * 10 + j) % 2520]++; } int wart = 0; if (cyfry[ile - i] > 1) { wart = (1 << (cyfry[ile - i] - 2)); } mask = (mask | wart); mod = (10 * mod + cyfry[ile - i]) % 2520; } for (int i = 0; i < 256; i++) { int mnoz = iloc(i); for (int j = 0; j < 2520; j += mnoz) { // info(ile, i, j); res += dp[ile][i][j]; } } long long res2 = cyfrowe(ile - 1); // cout << "resik " << res2 << endl; return res + distinct(x) + res2; } int main() { long long l, r; scanf("%lld%lld", &l, &r); long long big = smaller(r); long long small = smaller(l - 1); long long res = big - small; printf("%lld\n", res); } |