greedy math *1500

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n, s = map(int, input().split())
    p = 1
    while n % 10 == 0:
        n //= 10
        p *= 10
    a = 0
    while sum(map(int, str(n))) > s:
        d = n % 10
        a += (10 - d) * p
        n = (n + 10 - d)
        while n % 10 == 0:
            n //= 10
            p *= 10
    print(a)

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;


typedef long long int ll;
typedef unsigned long long ull;
typedef long double lld;


#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define float double
#define int long long
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define sz(x) ((int)(x).size())
#define prec(n) fixed<<setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define nline "\n"
#define debug(x) cout << #x <<" "; print(x); cout << endl;
 
 
void print(ll t) {cout << t;}
void print(string t) {cout << t;}
void print(char t) {cout << t;}
void print(lld t) {cout << t;}
void print(double t) {cout << t;}
void print(ull t) {cout << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(pair <T, V> p) {cout << "{"; print(p.ff); cout << ","; print(p.ss); cout << "}";}
template <class T> void print(vector <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(set <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(multiset <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T, class V> void print(map <T, V> v) {cout << "[ "; for (auto i : v) {print(i); cout << " ";} cout << "]";}
 
ll dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int N=2e5+5;
 
 
void solve()
{
    string s;
    ll k;
    cin>>s>>k;
    int n=sz(s);
    s.insert(s.begin(),'0');
    // debug(s)
    vll prefix(n+1,0);
    for (int i = 1; i <= n; i++)
    {
        prefix[i]=prefix[i-1]+(s[i]-'0');
    }
    // debug(prefix)

    if(prefix[n]<=k)
    {
        cout<<0<<endl;
        return;
    }
    int idx=0;
    for (int i = n; i >= 1; i--)
    {
        if(prefix[i-1]+1<=k)
        {
            idx=i;
            break;
        }
    }
    string res;
    int carry=0;
    for (int i = n; i >= idx; i--)
    {
        int num=((s[i]-'0')+carry);
        carry=num/10;
        num=num%10;
        int add=(num==0)? 0: 10-num;
        res.push_back((add+'0'));
        if(add!=0)
        carry=1;
    }
    while (res.size()>1 and res.back()=='0')
    {
        res.pop_back();
    }
    reverse(all(res));
    // debug(res)
    cout<<res<<endl;
}
 
 
 
int32_t main(){
              fast
              int t=1;
              cin>>t;
              // int temp = t;
              while(t--)
              {
//   cout<<"Case #"<<temp-t<<": ";
                  solve();
              }
return 0;
}


Comments

Submit
0 Comments
More Questions

831B - Keyboard Layouts
814A - An abandoned sentiment from past
268C - Beautiful Sets of Points
1391C - Cyclic Permutations
11A - Increasing Sequence
1406A - Subset Mex
1365F - Swaps Again
50B - Choosing Symbol Pairs
1719A - Chip Game
454B - Little Pony and Sort by Shift
1152A - Neko Finds Grapes
1719B - Mathematical Circus
1719C - Fighting Tournament
1642A - Hard Way
285C - Building Permutation
1719E - Fibonacci Strings
1696C - Fishingprince Plays With Array
1085A - Right-Left Cipher
1508B - Almost Sorted
1690C - Restoring the Duration of Tasks
1055A - Metro
1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String