An integer is considered beautiful if the sum of its digits is less than or equal to target.
Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful.
classSolution { longlongfind(longlong n){ longlong res = 0, p = 1; longlong carry = 0; while(n) { longlong now = carry + n % 10; carry = now / 10; now = now % 10; n /= 10; if(now) { res += (10 - now) * p; carry += 1; } p *= 10ll; } return res; } boolcheck(longlong n, int target){ longlong sum = 0; while(n) { sum += n % 10; n /= 10; } return sum <= target; } longlong __stoll(string& s) { longlong res = 0, p = 1; for(int i = s.length() - 1; i >= 0; i--) { res += (s[i] - '0') * p; p *= 10; } return res; } public: longlongmakeIntegerBeautiful(longlong n, int target){ longlong res = find(n); string s = to_string(res); while(s.length()) { string ns = s.substr(1); longlong now = __stoll(ns); if(check(now + n, target)) res = min(res,now); s = ns; }