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;
}
本文介绍了一个算法问题,即如何计算不超过n的特殊大数的数量,这些数的特点是其本身减去各位数字之和的差不小于s。文章通过观察差值规律并运用二分查找的方法找到了最小符合条件的大数,进而求出所有符合条件的大数数量。

557

被折叠的 条评论
为什么被折叠?



