Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- int dp[12][2][95];
- string s;
- int x;
- int solve(int pos, int isSmall, int digitSum) {
- if (pos == s.size()) {
- return (digitSum == x);
- }
- if (dp[pos][isSmall][digitSum] != -1) {
- return dp[pos][isSmall][digitSum];
- }
- int low = 0, high = s[pos] - '0';
- if (isSmall) {
- high = 9;
- }
- int res = 0;
- for (int i = low; i <= high; i++) {
- int new_sm = isSmall | (i < high);
- int val = solve(pos + 1, new_sm, digitSum + i);
- res += val;
- }
- return dp[pos][isSmall][digitSum] = res;
- }
- int main() {
- memset(dp, -1, sizeof(dp));
- cin >> s >> x;
- cout << solve(0, 0, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement