B

来源:互联网 发布:西地那非粉50克淘宝 编辑:程序博客网 时间:2024/04/30 08:09
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.

Example
Input

12 1Output3Input25 20Output0Input10 9Output1

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).

先理解题意,这就不写了;
主要是通过数字的分析慢慢的的找到规律。
1.每10个数发现,n-sum_digit(D) 都是相同的,例如10 11 12 .. 20 21 22 …
2.发现n-sum_digit(D),是递增的,这也是一个规律,并且发现增长的也是有点规律,就是n-sum_digit(D)是x*10的话,开始值只需要考虑[(x+1)*10,n];
3.发现不需要从1开始比较因为,difference是s,所以s-正数(n-sum_digit(D)) 肯定小于s,比s小也不需要,原因如上;

总:最后再来整体的看看这个题,发现difference是递增的,所以如果s>当前的n-sum_digit(D),肯定就是不存在的,自然是0;如果s==当前的n-sum_digit(D),这种就可以直接考虑包含这个数在内的 这十个数的情况:如果要是s<当前的n-sum_digit(D),这时候就需要我们去找到第一个符合s的值,然后相减就好了;这里的查找运用的二分,也可以直接暴力的计算,因为最坏的情况就是18个9,因为我们的判断条件是head-sum_digit>=s;所以s最大就是18*9,所以这个head就可以在很少的运算中就可以找到,开始的时候自己想到10^18,这样的话,就会超时,但是自己没能考虑到这个判断条件,s最大就是18*9,后面就没有必要算了,因为后面的n-sum_digit(D),肯定就会比s大,但是我们只是需要找到满足n-sum_digit(D)>=s,开始的条件,所以就是可以在几百个数的时候就可以找到了。

暴力查找

for(i=s+1;i<=n;i++)       {           if(i-he(i)>=s)              break;       }       if(i-he(i)>=s)            ans=n-i+1;        else            ans=0;

二分查找

#include<stdio.h>typedef long long ll;ll he(ll temp){    ll sum=0;    while(temp)    {       sum+=(temp%10);       temp/=10;    }    return sum;}int main(){    ll n,s,sum=0,ans,left,right,mid;    scanf("%lld%lld",&n,&s);    sum=he(n);    if(n-sum<s)        ans=0;    else if(n-sum==s)    {        ans=n%10+1;    }    else    {       left=s;right=n;       while(left<=right)       {          mid=(left+right)/2;          sum=he(mid);          if(mid-sum>=s)             right=mid-1;          else             left =mid+1;       }       ans=n-left+1;    }    printf("%lld\n",ans);    return 0;}