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).
题目大意:
找从1到n这些数中,有多少个数能够使得num-Dig(num)>=S.
这里Dig(num)表示num这个数每个位子上的数字和。
思路:
不难想到,如果对于一个数num来讲,有num-Dig(num)>=S.那么我们肯定就有num+1-Dig(num+1)>=S
那么这里显然就有有一个单调性,我们二分答案即可。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
int Slove(ll mid,ll s)
{
ll premid=mid;
ll tmp=0;
while(mid>0)
{
tmp+=mid%10ll;
mid/=10;
}
if(premid-tmp>=s)return 1;
else return 0;
}
int main()
{
ll n,s;
while(~scanf("%I64d%I64d",&n,&s))
{
ll ans=-1;
ll l=1;
ll r=n;
while(r-l>=0)
{
ll mid=(l+r)/2;
if(Slove(mid,s)==1)
{
r=mid-1;
ans=mid;
}
else l=mid+1;
}
if(ans==-1)printf("0\n");
else
printf("%I64d\n",n-ans+1);
}
}