链接: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;
}