QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620415#8932. BingozzpcdWA 86ms3632kbC++233.4kb2024-10-07 18:01:402024-10-07 18:01:41

Judging History

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

  • [2024-10-07 18:01:41]
  • 评测
  • 测评结果:WA
  • 用时:86ms
  • 内存:3632kb
  • [2024-10-07 18:01:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using s64 = long long;
using i128 = __int128_t;
struct gao{
    vector<int> a;
    gao(int x = 0) {
        while(x) {
            a.push_back(x % 10);
            x /= 10;
        }
        if(a.empty()) a.resize(1, 0);
    }
    gao(s64 x) {
        while(x) {
            a.push_back(x % 10);
            x /= 10;
        }
        if(a.empty()) a.resize(1, 0);
    }
    gao(string_view s) {
        for(auto it = s.rbegin(); it != s.rend(); it++) {
            a.push_back((*it) - '0');
        }
        if(a.empty()) a.resize(1, 0);
    }
    gao& operator+=(const gao& A) {
        // vector<int> tmp(max(a.size(), A.a.size()), 0);
        a.resize(max(a.size(), A.a.size()) + 2, 0);
        for(int i = 0; i + 1 < (int)a.size(); i++) {
            if(i < (int)A.a.size())
                a[i] += A.a[i];
            else if(A.a[i] < 10) break;
            if(a[i] >= 10) {
                a[i + 1]++;
                a[i] -= 10;
            }
        }
        while(a.size() > 1 && a.back() == 0) {
            a.pop_back();
        }
        return *this;
    }
    gao operator+(const gao& A) {
        auto tmp = *this;
        return tmp += A;
    }
    i128 subint(int l, int r) {
        i128 now = 0;
        for(r = min(r, (int)a.size() - 1); r >= l; r--)
            now = now * 10 + a[r];
        return now;
    }
    friend istream& operator>>(istream& is, gao& A) {
        static string tmp;
        cin >> tmp;
        A = gao(tmp);
        return is;
    }
    friend ostream& operator<<(ostream& os, const gao& A) {
        for(auto it = A.a.rbegin(); it != A.a.rend(); it++)
            os << (*it);
        return os;
    }
}n;
bool find(const gao& n, s64 m) {
    s64 M = m, now = 0, rt = 1;
    int len = 0;
    while(m) {
        m /= 10;
        len++;
    }
    len = max(1, len);
    if(n.a.size() < len) return false;
    for(int i = len - 1; i >= 0; i--)
        now = now * 10 + n.a[i], rt *= 10;
    rt /= 10;
    if(now == M) return true;
    for(int i = len; i < n.a.size(); i++) {
        now = now / 10 + rt * n.a[i];
        if(now == M) return true;
    }
    return false;
}
s64 m;
s64 Ans;
int lenm = 0;
void solve() {
    cin >> n >> m;
    lenm = 0;
    n += 1;
    {
        s64 tmpm = m;
        while(tmpm) {
            tmpm /= 10;
            lenm++;
        }
        if(lenm == 0) lenm = 1;
    }
    if(find(n, m)) {
        cout << n << '\n';
        return;
    }
    Ans = 0;
    for(int i = (int)n.a.size() - 1; i >= 0; i--) {
        Ans = (Ans * 10 + n.a[i]) % m;
    }
    if(Ans != 0) Ans = m - Ans;
    {
        s64 now = 0;
        for(int i = min((int)n.a.size(), lenm - 1); i >= 0; i--) {
            now = now * 10 + (9 - n.a[i]);
        }
        now++;
        if(find(n + now, m)) {
            Ans = min(Ans, now);
        }
    }
    i128 rt = 1;
    for(int i = 0; i <= 10; i++) {
        i128 tmp = n.subint(0, lenm + i - 1);
        // cerr << (s64)tmp << '\n';
        // cerr << (s64)(m * rt - tmp) << '\n';
        if(tmp <= rt * m && m * rt - tmp < Ans) {
            Ans = m * rt - tmp;
        }
        rt *= 10;

    }
    cout << n + Ans << '\n';

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3632kb

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
Wrong Answer
time: 86ms
memory: 3592kb

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:

3196282244
7614814251
2814581097
1075124424
7822266300
1767317769
7189709850
9061337569
6552679238
9946082160
5497675063
7787300365
4310767268
48113411000
3265496131
8294404062
2845521790
1114254674
6442013673
3744046867
3289624375
6477935360
1292587551
5504674689
2898829180
7882736025
2846033387
92...

result:

wrong answer 14th lines differ - expected: '4811342000', found: '48113411000'