比赛 2025.10.18 评测结果 RRRRRRRRRR
题目名称 WHZ 的数字 最终得分 0
用户昵称 wdsjl 运行时间 0.026 s
代码语言 C++ 内存使用 4.05 MiB
提交时间 2025-10-18 11:11:47
显示代码纯文本
#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;

ull cal(ull x) {
    if (x == 0) {
        return 1;
    }
    ull sum = 1; 
    ull wei = 1; 
    while (true) {
        ull div = wei * 10;
        if (div / 10 != wei) {
            break;
        }
        ull h = x / div;
        ull cur = (x / wei) % 10;
        ull low = x % wei;
        ull cnt = 0;
        if (h != 0) {
            if (cur > 0) {
                cnt = h * wei;
            } else {
                cnt = (h - 1) * wei + (low + 1);
            }
        }
        sum += cnt;
        ull nwei = wei * 10;
        if (nwei / 10 != wei) { 
            break;
        }
        wei = nwei;
        if (wei > x) {
            break;
        }
    }
    return sum;
}

ull ef(ull r, ull T) {
    ull l = 0;
    ull ans = 0;
    while (l <= r) {
        ull mid = l + (r - l) / 2; 
        ull fx = cal(mid);
        if (fx == T) {
            ans = mid;
            l = mid + 1; 
        } else if (fx < T) {
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    return ans;
}

int main() {
    freopen("whz_number4.in","r",stdin);
    freopen("whz_number4.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ull n, k;
    while (cin >> n >> k) {
        ull total = cal(n);
        ull T = total - k;
        if (T == 0) {
            cout << "0\n";
            continue;
        }
        ull r = n - 1;
        ull x = ef(r, T);
        cout << x + 1 << "\n";
    }
    return 0;
}