Codeforces Round #427 (Div. 2) B. The number on the board

本文介绍了一道算法题目,给定一个数k和一串自然数,目标是最少改变多少位数使得自然数总和大于等于k。文章提供了解决方案及AC代码。

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

B. The number on the board
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.

You have to find the minimum number of digits in which these two numbers can differ.

Input

The first line contains integer k (1 ≤ k ≤ 109).

The second line contains integer n (1 ≤ n < 10100000).

There are no leading zeros in n. It's guaranteed that this situation is possible.

Output

Print the minimum number of digits in which the initial number and n can differ.

Examples
input
3
11
output
1
input
3
99
output
0
Note

In the first example, the initial number could be 12.

In the second example the sum of the digits of n is not less than k. The initial number could be equal to n.


这道题题意就是先给你一个数k,然后给你一串自然数。求最少多少位自然数经过变化可以使整串自然数相加的和大于等于k。



AC代码:



#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
char k[1111111];
int cnt[11];
int main()
{
    long long int n;
    while(cin>>n)
    {
        scanf("%s",k);
        memset(cnt,0,sizeof(cnt));
        for(int i=0;k[i];i++)
        {
            n-=(k[i]-'0');
            cnt[k[i]-'0']++;
            if(n<=0)
                break;
        }
        if(n<=0)
        {
            cout<<0<<endl;
            continue;
        }
        int res=0;
        for(int i=0;i<=9;i++)
        {
            while(cnt[i])
            {
                n-=(9-i);
                res++;
                cnt[i]--;
                if(n<=0)
                    break;
            }
            if(n<=0)
                break;
        }
        cout<<res<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值