1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| int dfs(int pos, int pre, int lead, int limit) { if (!pos) { 边界条件 } if (!limit && !lead && dp[pos][pre] != -1) return dp[pos][pre]; int res = 0, up = limit ? a[pos] : 无限制位; for (int i = 0; i <= up; i ++) { if (不合法条件) continue; res += dfs(pos - 1, 未定参数, lead && !i, limit && i == up); } return limit ? res : (lead ? res : dp[pos][sum] = res); } int cal(int x) { memset(dp, -1, sizeof dp); len = 0; while (x) a[++ len] = x % 进制, x /= 进制; return dfs(len, 未定参数, 1, 1); } signed main() { cin >> l >> r; cout << cal(r) - cal(l - 1) << endl; }
|