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

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

先理解题意,这就不写了;
主要是通过数字的分析慢慢的的找到规律。
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值