题解 CF817C 【Really Big Numbers】

题目链接:CF817C

前置算法 : 二分

我们先考虑如何得到答案,若最小满足\(x\)减去其各数位之和后大于\(s\)

\(ans = n - x + 1\)

我们只要打个表就可以发现\(:\)

\(x < y\)\(|x| \leq |y|\) \((\)\(|x|\)表示\(x\)减去其各数位之和\()\)

证明就不写了

说明答案是递增的, 那就用二分

我们二分出最小满足\(|x|\)大于\(s\)

\(check\)函数就根据题意所写

如果找不到\(|x| \leq s\)就输出\(0\)

时间复杂度:\(O(log_2n \times log_{10}n)\)

\(Code:\)

#include <bits/stdc++.h>

#define ull unsigned long long

using namespace std;

ull Left, Right;
ull n, s, ans = 0x7fffffff / 3;

inline int check(ull mid) {
    ull now = mid;
    while (mid) {
        now -= mid % 10;
        mid /= 10;
    }
    return now >= s;
}

int main() {
    cin >> n >> s;
    Right = n;
    while (Right >= Left) {
        ull mid = Left + Right >> 1;
        if (check(mid)) {
            ans = mid;
            Right = mid - 1;
        }
        else Left = mid + 1;
    }
    printf("%lld", ans == 0x7fffffff / 3 ? 0 : n - ans + 1);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/chz-hc/p/12221319.html