-
Notifications
You must be signed in to change notification settings - Fork 0
/
D. Decrease the Sum of Digits.cpp
70 lines (67 loc) · 1.72 KB
/
D. Decrease the Sum of Digits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <fstream>
#include <string>
#include <iostream>
#include <math.h>
#include <algorithm>
#include<bits/stdc++.h>
#include <vector>
using namespace std;
int t;
long long n, s;
int main(){
cin >> t;
for (int z = 0; z < t; z++){
cin >> n >> s;
string ns = to_string(n);
string nd = "";
int tsum = 0;
for (int x = 0; x < ns.size(); x++){
tsum += (ns[x] - '0');
}
if (tsum <= s){
cout << 0 << "\n";
continue;
}
bool carry = false;
for (int x = ns.size() - 1; x >= 0; x--){
//cout << tsum << "\n";
//cout << ns << "\n";
if (x == 0){
int si = ns.size();
ns = "1";
for (int y = 0; y < si; y++){
ns += '0';
}
break;
}
if (ns[x] - '0' == 0){
if (!carry){
continue;
}
}
tsum -= (ns[x] - '0');
ns[x] = '0';
if (carry){
carry = false;
}
if (ns[x - 1] == '9'){
//cout << "yes\n";
carry = true;
ns[x - 1] = '0';
tsum -= 9;
}
else{
tsum += 1;
ns[x - 1] = ('0' + ((ns[x - 1] - '0') + 1));
}
if (!carry){
if (tsum <= s){
break;
}
}
}
//cout << ns << "\n";
string::size_type sz = 1000;
cout << stoll(ns, &sz, 10) - n << "\n";
}
}