Problem:
You are given a positive integer n. In one move, you can increase n by one (i.e. make n:=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 n be less than or equal to s.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and s (1≤n≤1018; 1≤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 n be less than or equal to s.
Example
input
5
2 1
1 1
500 4
217871987498122 10
100000000000000001 1
output
8
0
500
2128012501878
899999999999999999
题目大致意思为:输入n和s 你可以对n进行每次+1的操作 求最少多少次能让n的每个位上的数加起来不大于s
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
ll n, s;
cin >> n >> s;
vector <int> v;
ll nn = n;
while (nn)
{
v.push_back(nn % 10);
nn /= 10;
}
v.push_back(0);//补一位0 便于进位
ll sum = 0;
ll i;
for (i = v.size() - 1; i >= 0; i--)//从高位开始取
{
sum += v[i];
if (sum > s)//如果有位数取到了比s大的 或是多个位数加起来比s大的 退出循环
{
break;
}
}
if (sum <= s)//如果sum加起来的结果不大于s 说明不需要操作次数就可以满足题目要求
{
cout << 0 << endl;
continue;
}
ll j;
for (j = i; j <= v.size() - 1; j++)//进一位的操作
{
sum -= v[j];
v[j + 1]++;
sum++;
if (sum <= s)
{
break;
}
}
ll res = 0;
for (i = v.size() - 1; i >= 0; i--)//进一位后 满足题意的数字
{
res *= 10;
if (i > j)
{
res += v[i];
}
}
cout << res - n << endl;//满足题意的操作次数
}
return 0;
}