CodeForces - 817C 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

题意是给你两个数n和s,找出x(x<=n&&x-x每一位的和>=s)的数量。

 

  x                          x 每一位的和

0~9          -                    0~9                      =       0

10~19      -                  1~10                     =      9

20~ 29     -                   2~11                     =     9*2

……

100~109       -               1~ 10                  =      9*11

……

易发现x与x 的每一位的和的差值是递增的!!!(并不是严格规律递增,找规律不易做)

所以找出满足题意的最小的x,即可算出数量n-x+1。

差值是递增的,可用二分,找出最小的x

代码:

#include<cstdio>
using namespace std;
long long judge(long long k)
{
	long long sum=0;
	while(k)
	{
		sum+=k%10;
		k/=10;
	}
	return sum;
}
int main()
{
	long long n,s;
	scanf("%lld %lld",&n,&s);
	long long l=1,r=n;
	while(l<=r)
	{
		long long temp=(r+l)/2;
		if(temp-judge(temp)>=s)
			r=temp-1;
		else
			l=temp+1;
	}
//	printf("%lld %lld ",l,r);
	if(r-judge(r)>=s)
		printf("%lld\n",n-r+1);
	else
		printf("%lld\n",n-l+1);
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值