Try   HackMD

Codeforces 1409D. Decrease the Sum of Digits

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

#include<bits/stdc++.h> #define ll long long using namespace std; ll sumdigit(ll n){ ll temp=0; for(;n;n/=10) temp+=n%10; return temp; } ll solve(ll n,ll s){ if(sumdigit(n)<=s) return 0; if(n%10==0) return solve(n/10,s)*10; return solve(n+10-n%10,s)+10-n%10; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t; ll n,s; cin>>t; for(int i=0;i<t;++i){ cin>>n>>s; cout<<solve(n,s)<<endl; } return 0; }

思路:輸入的大數n每個位數相加必須<=給定的s,則每次取尾數出來算要加的值(10-n%10)然後加上之前的值