Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.
You have to find the minimum number of digits in which these two numbers can differ.
The first line contains integer k (1 ≤ k ≤ 109).
The second line contains integer n (1 ≤ n < 10100000).
There are no leading zeros in n. It's guaranteed that this situation is possible.
Print the minimum number of digits in which the initial number and n can differ.
3 11
1
3 99
0
In the first example, the initial number could be 12.
In the second example the sum of the digits of n is not less than k. The initial number could be equal to n.
题意:原本有一个数字x,它的各个数码的和原本是>=k的,现在这个数字x,在不改变位数的情况下,变成了n,问n和原来的数字x最少可能有多少位不一样
解题思路:如果各位大于等于k,直接输出0,否则,先把n小的数码加到9,一直加到大于等于k
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
char ch[100009];
int a[15];
LL k;
int main()
{
while (~scanf("%lld%s", &k, ch))
{
memset(a, 0, sizeof a);
int len = strlen(ch);
LL sum = 0;
for (int i = 0; i < len; i++) a[ch[i] - '0']++, sum += 1LL*(ch[i] - '0');
if (sum >= k) { printf("0\n"); continue; }
k -= sum;
int ans = 0;
for (int i = 0; i <= 9; i++)
{
if (k - a[i] * (9 - i) <= 0)
{
ans += k / (9 - i)+((k%(9-i))?1:0);
printf("%d\n", ans);break;
}
else ans += a[i];
k -= (9 - i)*a[i];
}
}
return 0;
}