Really Big Numbers .
Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are — in fact, he needs to calculate the 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 him in calculations.
Input
The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).
Output
Print one integer — the quantity of really big numbers that are not greater than n.
Examples
Input
12 1
Output
3
Input
25 20
Output
0
Input
10 9
Output
1
Note
In the first example numbers 10, 11 and 12 are really big.
In the second example there are no really big numbers that are not greater than 25(in fact, the first really big number is 30: 30 - 3 ≥ 20).
In the third example 10 is the only really big number (10 - 1 ≥ 9).
题意:如果一个数 x 减去 这个数各个位置之和 >= s , 这个数被定义为大数。 求 1 到 n 满足条件的大数 个数。
思路:设 f(x) = (x 各个位置的数之和)。 f(x + 1) = f(x) + 1
故 如果 x - f(x) >= s ===> x + 1 - f(x + 1) = x + 1 - f(x) - 1 = x - f(x) 。 也就是 x - f(x) >= s 成立, x 是大数,那么x+1 也是大数。 所以只需要 只需要找出 最小的大数 即可 答案就是 n - (最小的大数) + 1 ..... Orz, 看的大佬的、、、题解
二分寻找 最小的大数..
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f(ll x){
ll s = 0;
while(x){
s += x % 10;
x /= 10;
}
return s;
}
int main()
{
ll n,s; scanf("%I64d%I64d",&n,&s);
ll l = s,r = n;
while(l <= r){
ll mid = (l + r) >> 1;
if(mid - f(mid) >= s)
r = mid - 1;
else l = mid + 1;
}
if(r - f(r) >= s) ///while 循环 结束后 l > r, 如果 r 满足条件, r 是最小的大数
printf("%I64d\n",n - r + 1);
else{
if(l - f(l) >= s) printf("%I64d\n",n - l + 1); ///while 循环执行了,但是有可能 所有的mid 都并非 是大数, 需要判断一下
else printf("0\n");
}
return 0;
}