CodeForces - 817C Really Big Numbers

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值