You are given an integer . On each step, you may subtract one of the digits from the number.
How many steps are required to make the number equal to ?
Input
The only input line has an integer .
Output
Print one integer: the minimum number of steps.
Constraints
Example
Input:27
Output:5
Explanation: An optimal solution is .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. function | |
dp[i]: min step of to 0 | |
2. relation | |
dp[i] = min(1 + dp(i - d)) | |
3. base | |
dp[0] = 0 | |
*/ | |
int n; | |
vi memo; | |
vi get_digit(int x) { | |
vi res; | |
while (x > 0) { | |
res.push_back(x % 10); | |
x /= 10; | |
} | |
return res; | |
} | |
int dp(int i) { | |
if (i == 0) return 0; | |
if (memo[i] == -1) { | |
vi digits = get_digit(i); | |
int res = INT_MAX; | |
for (auto d : digits) { | |
if (d and i - d >= 0) res = min(res, 1 + dp(i - d)); | |
} | |
memo[i] = res; | |
} | |
return memo[i]; | |
} | |
void solve() { | |
// in | |
cin >> n; | |
memo.resize(n + 1, -1); | |
cout << dp(n) << endl; | |
} |
No comments:
Post a Comment