Ivan likes to learn different things aboutnumbers, but he is especially interested in really big numbers. Ivan thinksthat a positive integer number x is really big if the difference betweenx and the sum of its digits (in decimal representation) is not less thans. To prove that these numbers may have different special properties, hewants to know how rare (or not rare) they are — in fact, he needs to calculatethe quantity of really big numbers that are not greater than n.
Ivan tried to do the calculations himself,but soon realized that it's too difficult for him. So he asked you to help himin calculations.
Input
The first (and the only) line contains twointegers n and s (1 ≤ n, s ≤ 1018).
Output
Print one integer — the quantity of reallybig numbers that are not greater than n.
Examples
Input
12 1
Output
3
Input
25 20
Output
0
Input
10 9
Output
1
题目大意:求1 ~ n中满足(数字本身 - 数字各位数之和 >= s)的数字个数
思路:(数字本身 - 数字各位数之和 )呈现阶梯状的增长,即满足条件的点有最小值,二分查找最小的符合条件的端点,(n - 最小值 + 1)就能计算出数字个数
AC代码,__int64可用long long代替
#include <stdio.h>
#include <math.h>
__int64 judge(__int64 a, __int64 s)
{
__int64 m = 0, b = a;
while(a > 0)
{
m = m + a % 10;
a = a / 10;
}
if(b - m >= s) //判断是否满足条件
return 1;
return 0;
}
int main()
{
__int64 flag = -1;
__int64 n, s, num, l, r;
scanf("%I64d %I64d", &n, &s);
l = 1;
r = n;
__int64 mid;
while(r >= l)
{
mid = (l + r) >> 1;
if(judge(mid, s) == 1)
{
r = mid - 1;
flag = mid;
}
else
l = mid + 1;
}
if(flag == -1)
printf("0\n");
else
{
num = n - flag + 1;
printf("%I64d\n", num);
}
return 0;
}