记录编号 607679 评测结果 AAAAAAAAAA
题目名称 2949.[SYOI 2018] WHZ 的数字 最终得分 100
用户昵称 Gravatar淮淮清子 是否通过 通过
代码语言 C++ 运行时间 0.126 s
提交时间 2025-10-19 20:12:24 内存使用 3.73 MiB
显示代码纯文本
#include<iostream>
#include<algorithm>
using namespace std;

using ll = unsigned long long;

int count_zero(ll x) {
    if (x == 0) return 1;
    int cnt = 0;
    while (x) {
        if (x % 10 == 0) cnt++;
        x /= 10;
    }
    return cnt;
}

ll total_zero(ll a, ll b) {
    ll res = 0;
    for (ll i = a; i <= b; i++) {
        res += count_zero(i);
    }
    return res;
}

ll count(ll x){
    if(x < 0) return 0;
    if(x == 0) return 1;
    ll ans = 1, b = 1;
    while(b <= x){
        ll u = x % b, h = x / (b * 10);
        ll cur = (x / b) % 10;
        if(cur == 0) ans += (h - 1) * b + u + 1;
		else ans += h * b;
        b *= 10;
    }
    return ans;
}

ll n, k;

int main(){
	freopen("whz_number.in", "r", stdin);
	freopen("whz_number.out", "w", stdout);
	cin.tie(0); ios::sync_with_stdio(0);
    while(cin >> n >> k){
    	if (n <= 1000) {
            ll ans = 0;
            for (ll m = n; m >= 0; m--) {
                if (total_zero(m, n) == k) {
                    ans = m;
                    break;
                }
            }
            cout << ans << '\n';
            continue;
        }
        ll tot = count(n);
        ll tag = tot - k;
        ll l = 0, r = n, ans = -1;
        while(l <= r){
            ll mid = (l + r) / 2;
            if(count(mid) <= tag) ans = mid, l = mid + 1;
			else r = mid - 1;
        }
        cout << ans + 1 << '\n';
    }
    return 0;
}