题目传送门
/*
构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值。
另外,最多len-1次循环
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
int get_len(ll x) {
int ret = ;
while (x) {
x /= ; ret++;
}
return ret;
}
int get_nine(ll x) {
int ret = ;
while (x) {
ll y = x % ; x /= ;
if (y != ) break;
ret++;
}
return ret;
}
int main(void) { //Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!
//freopen ("C.in", "r", stdin);
ll p, d;
while (scanf ("%I64d%I64d", &p, &d) == ) {
int len = get_len (p); ll now = p;
int mx = get_nine (p); ll ans = p;
ll cut = ;
while (true) {
ll y = now / cut % ;
if (y != ) {
now = now - cut * ; now = now / cut + cut - ;
}
cut *= ;
if (now < p - d) break;
int cnt = get_nine (now);
if (cnt > mx) ans = now;
}
printf ("%I64d\n", ans);
}
return ;
}