QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562796#6303. InversionHTensor#TL 0ms0kbC++172.4kb2024-09-13 21:02:542024-09-13 21:02:57

Judging History

你现在查看的是最新测评结果

  • [2024-09-13 21:02:57]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-13 21:02:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int __int128

istream &operator>>(istream &is, __int128 &x) {
    string s;
    is >> s;
    bool neg = false;
    x = 0;
    for (char c : s) {
        if (c == '-') neg = true;
        else x = x * 10 + (c - '0');
    }
    if (neg) x = -x;
    return is;
}
ostream &operator<<(ostream &os, __int128 x) {
    if (x == 0) os << 0;
    else {
        string s, t;
        if (x < 0) x = -x, t = "-";
        while (x) s.push_back('0' + x % 10), x /= 10;
        reverse(s.begin(), s.end());
        os << t << s;
    }
    return os;
}

void solve() {
    int x, k;
    cin >> x >> k;

    int a[40]{};
    
    int pw[40]{};
    pw[0] = 1;

    int tx = x;
    for (int i = 0; i < 36; i++) {
        a[i] = tx % 10;
        tx /= 10;
        pw[i + 1] = pw[i] * 10;
    }

    if (k == 0) {
        for (int i = 0; i < 36; i++) {
            if (a[i] != 9) {
                cout << 1;
                for (int j = 0; j < i; j++) {
                    cout << 0;
                }
                cout << "\n";
                return;
            }
        }
    }

    vector dp(36, vector(20, vector<int>(2, -1)));
    for (int i = 0; i < 36; i++) {
        dp[i][0][0] = 0;
        if (a[i] != 0) {
            dp[i][1][1] = (10 - a[i]) * pw[i];
        }
    }

    auto chmin = [&](int &a, int b) {
        if (a == -1) {
            a = b;
        } else if (b != -1) {
            a = min(a, b);
        }
    };

    for (int i = 1; i < 36; i++) {
        for (int j = 1; j <= i + 1; j++) {
            if (j > 18) {
                break;
            }
            chmin(dp[i][j][0], dp[i - 1][j][0]);
            if (a[i] != 9) {
                chmin(dp[i][j][0],dp[i - 1][j][1]);
            }
            if (dp[i - 1][j - 1][1] != -1) {
                chmin(dp[i][j][1], dp[i - 1][j - 1][1] + (9 - a[i]) * pw[i]);
            }
            if (a[i] != 0 && dp[i - 1][j - 1][0] != -1) {
                chmin(dp[i][j][1], dp[i - 1][j - 1][0] + (10 - a[i]) * pw[i]);
            }
        }
    }


    int ans = -1;
    for (int t = 0; t < 2; t++) {
        chmin(ans, dp[35][k][t]);
    }
    
    cout << ans << "\n";
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T; cin >> T;
    while(T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3

output:


result: