QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#607935#8932. BingoUESTC_NLNSRE 1ms3516kbC++202.3kb2024-10-03 17:10:142024-10-03 17:10:14

Judging History

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

  • [2024-10-03 17:10:14]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3516kb
  • [2024-10-03 17:10:14]
  • 提交

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, k; --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 (i128 i = n.size() - 1, c = 1; i >= 0; --i, c = 1ll * c * 10) {
        ans = ans + c * (n[i] - '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;
}

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

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

    n = "0" + n;

    add(n, 1);
    string sm = i2s(m);
    int lm = sm.size();
    if (n.find(i2s(m)) != string::npos) {
        print(n);
        cout << '\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 - 5), 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 = stoi(s1);
        if (m1 >= n2i) {
            off = min(off, ll(m1 - n2i));
        }
    }
    add(n, off);
    print(n);
    cout << '\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: 100
Accepted
time: 1ms
memory: 3516kb

input:

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

output:

9
13
10
251
1370
3

result:

ok 6 lines

Test #2:

score: -100
Runtime Error

input:

100000
3196282243 28
7614814237 33
2814581084 97
1075124401 58
7822266214 100
1767317768 31
7189709841 75
9061337538 69
6552679231 38
9946082148 18
5497675062 54
7787300351 65
4310767261 68
4811341953 100
3265496130 31
8294404054 62
2845521744 90
1114254672 26
6442013672 13
3744046866 40
3289624367 ...

output:


result: