A - Really Big Numbers 二分

Ivan likes to learn different things aboutnumbers, but he is especially interested in really big numbers. Ivan thinksthat a positive integer number x is really big if the difference betweenx and the sum of its digits (in decimal representation) is not less thans. To prove that these numbers may have different special properties, hewants to know how rare (or not rare) they are — in fact, he needs to calculatethe 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 himin calculations.

Input

The first (and the only) line contains twointegers n and s (1 ≤ n, s ≤ 1018).

Output

Print one integer — the quantity of reallybig numbers that are not greater than n.

Examples

Input

12 1

Output

3

Input

25 20

Output

0

Input

10 9

Output

1

 题目大意:求1 ~ n中满足(数字本身 - 数字各位数之和 >= s)的数字个数

思路:(数字本身 - 数字各位数之和 )呈现阶梯状的增长,即满足条件的点有最小值,二分查找最小的符合条件的端点,(n - 最小值 + 1)就能计算出数字个数

AC代码,__int64可用long long代替

#include <stdio.h>
#include <math.h>

__int64 judge(__int64 a, __int64 s)
{
	__int64 m = 0, b = a;

	while(a > 0)
        {
          m = m + a % 10;
          a = a / 10;
        }

	if(b - m >= s)      //判断是否满足条件
	    return 1;
	return 0;
}

int main()
{
	__int64 flag = -1;
	__int64 n, s, num, l, r;
	scanf("%I64d %I64d", &n, &s);
	l = 1;
	r = n;
	__int64 mid;
	while(r >= l)
	{
		mid = (l + r) >> 1;
		if(judge(mid, s) == 1)
		{
			r = mid - 1;
			flag = mid;
		}
		else
			l = mid + 1;
	}
	if(flag == -1)
		printf("0\n");
	else
	{	
		num = n - flag + 1;
		printf("%I64d\n", num);
	}	
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值