这个题好像也是贪心的感觉。。
我们枚举 $1,5,10,50,100,\dots$ ,找出在 $[l, r]$ 内能整除它们的最小的数。
然后找到其中在荒谬值最小的情况下数值最小的那个数,
就做完了。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
#define INF 593119681 int _;
LL l, r; inline LL Calc(LL x)
{
if (!~x) return INF;
while (x % == ) x /= ;
int f = (x % ) ? : -;
int res = ;
for (; x; x /= )
res += ;
return res + f;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("4029.in", "r", stdin);
freopen("4029.out", "w", stdout);
#endif for (scanf("%d", &_); _; _ --)
{
scanf("%lld%lld", &l, &r);
LL best = -, top, bot;
for (LL x = , _x = ; x <= r; x *= , _x *= )
{
top = (l + x - ) / x * x;
bot = r / x * x;
if (top <= bot) best = (Calc(best) < Calc(top) || (Calc(best) == Calc(top) && best < top)) ? best : top;
top = (l + _x - ) / _x * _x;
bot = r / _x * _x;
if (top <= bot) best = (Calc(best) < Calc(top) || (Calc(best) == Calc(top) && best < top)) ? best : top;
}
printf("%lld\n", best);
} #ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
4029_Gromah