D. Decrease the Sum of Digits

194 篇文章 1 订阅
71 篇文章 0 订阅

链接:https://codeforces.ml/problemset/problem/1409/D

You are given a positive integer nn. In one move, you can increase nn by one (i.e. make n:=n+1n:=n+1). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of nn be less than or equal to ss.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

The only line of the test case contains two integers nn and ss (1≤n≤10181≤n≤1018; 1≤s≤1621≤s≤162).

Output

For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of nn be less than or equal to ss.

Example

input

5
2 1
1 1
500 4
217871987498122 10
100000000000000001 1

output

8
0
500
2128012501878
899999999999999999

代码:

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
ll n,m,u,v,x,t;
ll a[maxn];
ll s;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>s;
        u=0;
        m=0;
        memset(a,0,sizeof(a));
        while(n)
        {
            a[u]=n%10;
            n/=10;
            m+=a[u];
            u++;
        }
        if(m<=s)
            cout<<0<<endl;
        else
        {
            ll ans=0,k=1,flag=0;
            for(int i=0;i<u;i++)
            {
                a[i]+=flag;
                if(a[i]==0)
                {
                    k*=10;
                    continue;
                }
                if(a[i]>=10)
                {
                    a[i+1]+=a[i]/10;
                    a[i]%=10;
                }
                ans+=(10-a[i])*k;
                m=0;
                a[i]=10;
                for(int j=0;j<u;j++)
                {
                    if(a[j]<=9)
                    m+=a[j];
                    else
                    {
                        a[j+1]++;
                        a[j]%=10;
                        m+=a[j];
                        if(j==u-1)
                            u++;
                    }
                }
                if(m<=s)
                    break;
                    k*=10;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值