D. Decrease the Sum of Digits Codeforces Round 667 (Div. 3)

You are given a positive integer n�. In one move, you can increase n� by one (i.e. make n:=n+1�:=�+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⋅1041≤�≤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≤10181≤�≤1018; 1≤s≤1621≤�≤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

题目大意:

读入两个数x,y,问使x每个位置上的数字和小于等于y至少要加多少?

 思路:

贪心,从前往后遍历,x每个位置上的数之和小于等于y,那么还不需要加数值。

但x每一位数上的和等于y时,记录x此时数字的位置,因为当后面还有数字时,那么x此时的位置也需要进位。

当x有一位数上的和大于y时,这一位数和后面的所有数都需要进位。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"
 
void solve(){
	ll x=0,y,z=0,w,jl=-1;
	bool f=0;
	string s,jg=""; 
	cin >> s >> y;
	for(ll i = 0 ; i < s.size() ; i ++){
		x+=s[i]-'0';
		if(x == y && jl < 0)jl=i;
		if(x > y){
			jl != -1 ? w = jl : w = i;
			break;
		}
	}
	if(y >= x){
		cout << 0 << endl;
		return;
	}
	for(ll i = s.size()-1 ; i >= w ; i --){
		if(z == 0 && i == w && x == y)break;
		if(z == 0)jg=to_string((10-(s[i]-'0'))%10)+jg;
		else jg=to_string((10-(s[i]-'0')-1)%10)+jg;
		z+=s[i]-'0';
	}
	while(jg.size() > 1 && jg[0] == '0')jg.erase(jg.begin());//清除前导0 
	cout << jg << endl;
	return;
}
 
int main(){
	ll t=1;cin >> t;
	while(t--)solve();
	return 0;
}

相关推荐

  1. D. Decrease the Sum of Digits Codeforces Round 667 (Div. 3)

    2024-03-24 18:02:02       33 阅读
  2. cf-913-div3

    2024-03-24 18:02:02       67 阅读
  3. Codeforces Round 909 (Div. 3)

    2024-03-24 18:02:02       59 阅读
  4. Codeforces Round 920 (Div. 3)

    2024-03-24 18:02:02       56 阅读
  5. Codeforces Round 481 (Div. 3)

    2024-03-24 18:02:02       59 阅读
  6. Codeforces Round 925 (Div. 3)

    2024-03-24 18:02:02       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-24 18:02:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-24 18:02:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-24 18:02:02       82 阅读
  4. Python语言-面向对象

    2024-03-24 18:02:02       91 阅读

热门阅读

  1. 《PHP 魔法之旅续:探索高级技术与优化技巧》

    2024-03-24 18:02:02       37 阅读
  2. c++结束输入

    2024-03-24 18:02:02       34 阅读
  3. MongoDB优化

    2024-03-24 18:02:02       41 阅读
  4. 数仓,大数据平台,数据中台,数据湖

    2024-03-24 18:02:02       31 阅读
  5. 基础时间线柱状图绘制

    2024-03-24 18:02:02       37 阅读
  6. docker小白采坑---启动失败---空间不足

    2024-03-24 18:02:02       38 阅读
  7. Docker Compose 中spring boot服务连接 mysql

    2024-03-24 18:02:02       40 阅读
  8. 数据结构奇妙旅程之链表

    2024-03-24 18:02:02       43 阅读
  9. c语言函数大全(D开头)

    2024-03-24 18:02:02       35 阅读
  10. 美国硅谷大带宽服务器怎么样

    2024-03-24 18:02:02       35 阅读