fork download
  1. #include<unordered_map>
  2. #include<unordered_set>
  3. #include<functional>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<hash_map>
  7. #include<iterator>
  8. #include<iomanip>
  9. #include<cstring>
  10. #include<vector>
  11. #include<string>
  12. #include<deque>
  13. #include<queue>
  14. #include<array>
  15. #include<cmath>
  16. #include<list>
  17. #include<map>
  18. #include<set>
  19. using namespace std;
  20. typedef long long ll;
  21. typedef unsigned long long ull;
  22. typedef double db;
  23. #define pii pair<int,int>
  24. #define pll pair<ll,ll>
  25. #define inf INT32_MAX
  26. #define pf push_front
  27. #define pb push_back
  28. #define ppb pop_back
  29. #define ff first
  30. #define ss second
  31. const int mod=1e9+7;
  32. int main(){
  33. ios_base::sync_with_stdio(false);
  34. cin.tie(nullptr);
  35. cout.tie(nullptr);
  36.  
  37. int t;
  38. cin>>t;
  39. string d;
  40. ll n,s,cnt;
  41. for(;t;--t){
  42. cnt=0;
  43. cin>>d>>s,n=stoll(d);
  44. reverse(d.begin(),d.end());
  45. for(auto i:d)
  46. cnt+=i-'0';
  47. if(cnt<=s){
  48. cout<<0<<'\n';
  49. continue;
  50. }
  51. int idx=0;
  52. while(cnt>s){
  53. cnt-=d[idx]-'0',d[idx++]='0';
  54. if(idx==d.size()){
  55. d.pb('1');
  56. cnt++;
  57. continue;
  58. }
  59. if(d[idx]!='9'){
  60. cnt++,d[idx]++;
  61. continue;
  62. }
  63. while(d[idx]=='9'){
  64. cnt-=d[idx]-'0';
  65. if(idx==d.size()-1)d.pb('1');
  66. d[idx++]='0';
  67. }
  68. cnt++;
  69. }
  70. reverse(d.begin(),d.end());
  71. cout<<stoll(d)-n<<'\n';
  72. }
  73. }
Runtime error #stdin #stdout #stderr 0s 4544KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoll