Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
875 B
2
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
 
const double PI = 3.14159265358979;
const ll INF =  1e18 + 7;
const ll MOD = 1e9 + 7;
const ll nax = 1000005;
const int LOG = 25;


bool check(ll x, ll s) {
    ll sumOfDigits = 0;
    ll y = x;
    while(y > 0) {
        sumOfDigits += (y % 10);
        y /= 10;
    }
    return x - sumOfDigits >= s;
}

void solve() {
   ll n, s;
   cin >> n >> s;
   ll left = 1, right = n, ans = 0;
   while(left <= right) {
        ll mid = (left + right) / 2;
        if (check(mid, s)) {
            right = mid - 1;
        } else {
            ans = mid;
            left = mid + 1;
        }
   }
   cout << n - ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t; cin >> t; while(t--)
    solve();
    return 0;
}
Leave a Comment