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.
The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).
Print one integer — the quantity of really big numbers that are not greater than n.
12 1
3
25 20
0
10 9
1
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).
令f(x)为x-(x各位数之和)
可以看出f(x)是单调不减的
设y>x
若x 与 y只有个位不同 则f(x)==f(y)
反之f(x)>f(y)
若x各位数之和小于y 则必然成立
若x各位数之和等于y 因为x>y 所以成立
若x各位数之和大于y 则x至少有一位大于y对应的那一位
设只有一位
设其为右数第i位 则从最后一位-第i-1位f(y)-f(x)>0
又因为x各位数之和大于y所以到第i位f(x)-f(y)至少为(j+1)*10^(i-1)-j j为i位之后 y各位和-x各位和
显然成立
若有多位 则可把所有这样的位合并
同一位
所以可以二分答案(二分出合法的 再算出不合法的)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
inline LL get(LL x){
LL ans=0;
while(x){
ans+=x%10;
x/=10;
}
return ans;
}
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
LL n,s;
scanf("%I64d %I64d",&n,&s);
LL l=0,r=n;
while(l+1<r){
LL mid=(l+r)>>1;
if(mid-get(mid)<s)
l=mid;
else r=mid-1;
}
if(r-get(r)<s)
printf("%I64d\n",n-r);
else printf("%I64d\n",n-l);
return 0;
}