C. Unlucky Numbers(数位dp)

199 阅读2分钟

题目

In this problem, unlike problem A, you need to look for unluckiest number, not the luckiest one.

Note that the constraints of this problem differ from such in problem A.

Olympus City recently launched the production of personal starships. Now everyone on Mars can buy one and fly to other planets inexpensively.

Each starship has a number —some positive integer xx. Let's define the luckiness of a number xx as the difference between the largest and smallest digits of that number. For example, 142857142857 has 88 as its largest digit and 11 as its smallest digit, so its luckiness is 81=78-1=7. And the number 111111 has all digits equal to 11, so its luckiness is zero.

Hateehc is a famous Martian blogger who often flies to different corners of the solar system. To release interesting videos even faster, he decided to buy himself a starship. When he came to the store, he saw starships with numbers from ll to rr inclusively. While in the store, Hateehc wanted to find a starship with the unluckiest number.

Since there are a lot of starships in the store, and Hateehc can't program, you have to help the blogger and write a program that answers his question.

输入

The first line contains an integer tt (1t6001 \le t \le 600) —the number of test cases.

Each of the following tt lines contains a description of the test case. The description consists of two integers ll, rr (1lr10181 \le l \le r \le 10^{18}) — the largest and smallest numbers of the starships in the store.

Code

/*******************************
| Author:  chzu_w
| Problem: C. Unlucky Numbers
| Contest: Codeforces Round 861 (Div. 2)
| URL:     https://codeforces.com/contest/1808/problem/C
| When:    2023-03-29 17:09:53
| 
| Memory:  256 MB
| Time:    2000 ms
*******************************/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define pb push_back
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef vector<int> VI;
typedef pair<int,int> PII;
ll MOD;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
mt19937 mrand(random_device{}()); 
const int N = 2e5 + 10;
int fac[N];
int a[N], b[N];
vector<vector<vector<PII>>> f (20, vector<vector<PII>>(10, vector<PII>(10, {-1, -1})));
void solve()
{
    int l, r; cin >> l >> r;
    int lena = 0, lenb = 0;
    while(r) {
        b[++lenb] = r % 10;
        r /= 10;
    }
    for (int i = 1; i <= lenb; i++) a[i] = 0;
    while(l) {
        a[++lena] = l % 10;
        l /= 10;
    }
    
     function<PII(int, bool, bool, int, int, int, bool)> dfs = [&] (int pos, bool limitup, bool limitdown, int mx, int mn, int pre, bool pre0) -> PII {
        if (!pos) return {pre, mx - mn};
        if (!limitup && !limitdown && f[pos][mx][mn] != make_pair(-1ll, -1ll) && !pre0) return f[pos][mx][mn];
        int up = limitup ? b[pos] : 9;
        int down = limitdown ? a[pos] : 0;
        PII res = {-1, 10};
        for (int i = down; i <= up; i++) {
            bool flag = pre0 && i == 0;
            int mmx = flag ? 0 : max(mx, i);
            int mmn = flag ? 9 : min(mn, i);
            auto [x, y] = dfs(pos -1, limitup && i == up, limitdown && i == down, mmx, mmn, i, flag);
            if (y < res.se) {
                res = {pre * fac[pos] + x, y};
            }
        }
        if (!limitup && !limitdown && !pre0) f[pos][mx][mn] = res;
        return res;
    };
    cout << dfs(lenb, true, true, 0, 9, 0, true).fi << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    fac[0] = 1;
    for (int i = 1; i <= 18; i++) fac[i] = fac[i - 1] * 10;
    int T;cin >> T;
    while ( T -- )
    solve();
    return 0;
}