QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#607961#8932. BingoUESTC_NLNSWA 0ms3800kbC++202.2kb2024-10-03 17:22:002024-10-03 17:22:00

Judging History

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

  • [2024-10-03 17:22:00]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3800kb
  • [2024-10-03 17:22:00]
  • 提交

answer

#include <iostream>
#include <sstream>
using namespace std;

using ll = long long;

const string o = "000000000000000000000000000000000000000000000000000000000000";

void add(string& n, ll k) {
    for (int i = n.size() - 1; i >= 0; --i) {
        n[i] = n[i] + k % 10;
        k /= 10;
        if (n[i] > '9') {
            n[i] -= 10;
            k++;
        }
    }
}

using i128 = __int128_t;
i128 s2i(const string& n) {
    i128 ans = 0;
    for (const char u : n) ans = ans * 10 + (u - '0');
    return ans;
}

int cmp(const string& n, const int m) {
    if (n.size() > 9) return 1;
    ll n1 = s2i(n);
    if (n1 > m) return 1;
    if (n1 == m) return 0;
    return -1;
}

int cmp(const string& n, const string& m) {
    if (n.size() != m.size()) return (n.size() < m.size() ? -1 : 1);
    for (int i = 0; i < n.size(); ++i) {
        if (n[i] != m[i]) return (n[i] < m[i] ? -1 : 1);
    }
    return 0;
}

string i2s(const ll m) {
    stringstream ss;

    ss << m;
    string ans;
    ss >> ans;
    return ans;
}

void print(const string& a) {
    if (a[0] == '0')
        cout << a.substr(1);
    else
        cout << a;
    cout << '\n';
}

void solve() {
    string n;
    int m;
    string sm = i2s(m);
    cin >> n >> m;

    if (cmp(n, m) == -1) {
        cout << m << '\n';
        return;
    }

    n = "0" + n;

    add(n, 1);
    int lm = sm.size();
    if (n.find(i2s(m)) != string::npos) {
        print(n);
        return;
    }
    int r = 0;
    for (int i = n.size() - 1, c = 1; i >= 0; --i, c = 1ll * c * 10 % m) {
        r = (r + 1ll * c * (n[i] - '0')) % m;
    }
    ll off = r == 0 ? 0 : m - r;

    string n2 = n.substr(max(int(n.size() - 2 * lm - 12), 0));
    i128 n2i = s2i(n2);
    for (int i = 0; i <= n2.size() - lm; ++i) {
        string s1 = n2.substr(0, i) + sm + o.substr(0, n2.size() - lm - i);
        i128 m1 = s2i(s1);
        if (m1 >= n2i && m1 - n2i < m) {
            off = min(off, ll(m1 - n2i));
        }
    }
    add(n, off);
    print(n);
}

int main() {

    // cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--) solve();
}

/*
6
7 3
12 3
9 10
249 51
1369 37
2 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3800kb

input:

6
7 3
12 3
9 10
249 51
1369 37
2 1

output:

9
13
10
255
1370
3

result:

wrong answer 4th lines differ - expected: '251', found: '255'