Advertisement
tungSfer

tong chu so

May 30th, 2022
1,813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define pb push_back
  5. #define pii pair<int, int>
  6. #define pll pair<ll, ll>
  7. #define el endl
  8. #define umi unordered_map<int, int>
  9. #define umll unordered_map<ll, ll>
  10. #define all(vect) vect.begin(), vect.end()
  11. #define reset(A) memset(A, 0, sizeof(A))
  12. #define approx(n) fixed << setprecision(n)
  13.  
  14. using namespace std;
  15.  
  16. const int mod = 1e9 + 7;
  17.  
  18. ll sumOfDigits(ll n) {
  19.     ll sum = 0;
  20.     while(n) {
  21.         sum += n % 10;
  22.         n /= 10;
  23.     }
  24.     return sum;
  25. }
  26.  
  27. void solve()
  28. {
  29.   ll n, s;
  30.   cin >> n >> s;
  31.   if(sumOfDigits(n) <= s)
  32.   {
  33.     cout << 0 << el;
  34.     return;
  35.   }
  36.   int i = 1;
  37.   ll p = (ll)pow(10, 1);
  38.   while(p < n) {
  39.     i++;
  40.     p = (ll)pow(10, i);
  41.   }
  42.   if(i % 2 == 0) {
  43.     p++;
  44.   }
  45.   // cout << p - n << el;
  46.   p -= n;
  47.   ll ind = 1;
  48.   while(true) {
  49.     ll te = (ll)pow(10, ind);
  50.     if(sumOfDigits(n + p % te) <= s) {
  51.       cout << p % te << el;
  52.       return;
  53.     }
  54.     ind++;
  55.   }
  56. }
  57.  
  58. int main()
  59. {
  60.   int t = 1;
  61.   cin >> t;
  62.   // cin.ignore();
  63.   while (t--)
  64.   {
  65.     solve();
  66.   }
  67.   return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement