QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#539387#8932. Bingoucup-team112#WA 91ms5104kbC++2017.3kb2024-08-31 14:40:122024-08-31 14:40:14

Judging History

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

  • [2024-08-31 14:40:14]
  • 评测
  • 测评结果:WA
  • 用时:91ms
  • 内存:5104kb
  • [2024-08-31 14:40:12]
  • 提交

answer

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #define INTERACTIVE

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

namespace templates {
// type
using ll  = long long;
using ull = unsigned long long;
using Pii = pair<int, int>;
using Pil = pair<int, ll>;
using Pli = pair<ll, int>;
using Pll = pair<ll, ll>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using qp = priority_queue<T, vector<T>, greater<T>>;
// clang-format off
#define vec(T, A, ...) vector<T> A(__VA_ARGS__);
#define vvec(T, A, h, ...) vector<vector<T>> A(h, vector<T>(__VA_ARGS__));
#define vvvec(T, A, h1, h2, ...) vector<vector<vector<T>>> A(h1, vector<vector<T>>(h2, vector<T>(__VA_ARGS__)));
// clang-format on

// for loop
#define fori1(a) for (ll _ = 0; _ < (a); _++)
#define fori2(i, a) for (ll i = 0; i < (a); i++)
#define fori3(i, a, b) for (ll i = (a); i < (b); i++)
#define fori4(i, a, b, c) for (ll i = (a); ((c) > 0 || i > (b)) && ((c) < 0 || i < (b)); i += (c))
#define overload4(a, b, c, d, e, ...) e
#define fori(...) overload4(__VA_ARGS__, fori4, fori3, fori2, fori1)(__VA_ARGS__)

// declare and input
// clang-format off
#define INT(...) int __VA_ARGS__; inp(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; inp(__VA_ARGS__);
#define STRING(...) string __VA_ARGS__; inp(__VA_ARGS__);
#define CHAR(...) char __VA_ARGS__; inp(__VA_ARGS__);
#define DOUBLE(...) double __VA_ARGS__; STRING(str___); __VA_ARGS__ = stod(str___);
#define VEC(T, A, n) vector<T> A(n); inp(A);
#define VVEC(T, A, n, m) vector<vector<T>> A(n, vector<T>(m)); inp(A);
// clang-format on

// const value
const ll MOD1   = 1000000007;
const ll MOD9   = 998244353;
const double PI = acos(-1);

// other macro
#if !defined(RIN__LOCAL) && !defined(INTERACTIVE)
#define endl "\n"
#endif
#define spa ' '
#define len(A) ll(A.size())
#define all(A) begin(A), end(A)

// function
vector<char> stoc(string &S) {
    int n = S.size();
    vector<char> ret(n);
    for (int i = 0; i < n; i++) ret[i] = S[i];
    return ret;
}
string ctos(vector<char> &S) {
    int n      = S.size();
    string ret = "";
    for (int i = 0; i < n; i++) ret += S[i];
    return ret;
}

template <class T>
auto min(const T &a) {
    return *min_element(all(a));
}
template <class T>
auto max(const T &a) {
    return *max_element(all(a));
}
template <class T, class S>
auto clamp(T &a, const S &l, const S &r) {
    return (a > r ? r : a < l ? l : a);
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
    return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
    return (a > b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chclamp(T &a, const S &l, const S &r) {
    auto b = clamp(a, l, r);
    return (a != b ? a = b, 1 : 0);
}

template <typename T>
T sum(vector<T> &A) {
    T tot = 0;
    for (auto a : A) tot += a;
    return tot;
}

template <typename T>
vector<T> compression(vector<T> X) {
    sort(all(X));
    X.erase(unique(all(X)), X.end());
    return X;
}

// input and output
namespace io {
// __int128_t
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
    std::ostream::sentry s(dest);
    if (s) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[128];
        char *d = std::end(buffer);
        do {
            --d;
            *d = "0123456789"[tmp % 10];
            tmp /= 10;
        } while (tmp != 0);
        if (value < 0) {
            --d;
            *d = '-';
        }
        int len = std::end(buffer) - d;
        if (dest.rdbuf()->sputn(d, len) != len) {
            dest.setstate(std::ios_base::badbit);
        }
    }
    return dest;
}

// vector<T>
template <typename T>
istream &operator>>(istream &is, vector<T> &A) {
    for (auto &a : A) is >> a;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &A) {
    for (size_t i = 0; i < A.size(); i++) {
        os << A[i];
        if (i != A.size() - 1) os << ' ';
    }
    return os;
}

// vector<vector<T>>
template <typename T>
istream &operator>>(istream &is, vector<vector<T>> &A) {
    for (auto &a : A) is >> a;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<vector<T>> &A) {
    for (size_t i = 0; i < A.size(); i++) {
        os << A[i];
        if (i != A.size() - 1) os << endl;
    }
    return os;
}

// pair<S, T>
template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &A) {
    is >> A.first >> A.second;
    return is;
}
template <typename S, typename T>
ostream &operator<<(ostream &os, pair<S, T> &A) {
    os << A.first << ' ' << A.second;
    return os;
}

// vector<pair<S, T>>
template <typename S, typename T>
istream &operator>>(istream &is, vector<pair<S, T>> &A) {
    for (size_t i = 0; i < A.size(); i++) {
        is >> A[i];
    }
    return is;
}
template <typename S, typename T>
ostream &operator<<(ostream &os, vector<pair<S, T>> &A) {
    for (size_t i = 0; i < A.size(); i++) {
        os << A[i];
        if (i != A.size() - 1) os << endl;
    }
    return os;
}

// tuple
template <typename T, size_t N>
struct TuplePrint {
    static ostream &print(ostream &os, const T &t) {
        TuplePrint<T, N - 1>::print(os, t);
        os << ' ' << get<N - 1>(t);
        return os;
    }
};
template <typename T>
struct TuplePrint<T, 1> {
    static ostream &print(ostream &os, const T &t) {
        os << get<0>(t);
        return os;
    }
};
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
    TuplePrint<decltype(t), sizeof...(Args)>::print(os, t);
    return os;
}

// io functions
void FLUSH() {
    cout << flush;
}

void print() {
    cout << endl;
}
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
    cout << head;
    if (sizeof...(Tail)) cout << spa;
    print(std::forward<Tail>(tail)...);
}

template <typename T, typename S>
void prisep(vector<T> &A, S sep) {
    int n = A.size();
    for (int i = 0; i < n; i++) {
        cout << A[i];
        if (i != n - 1) cout << sep;
    }
    cout << endl;
}
template <typename T, typename S>
void priend(T A, S end) {
    cout << A << end;
}
template <typename T>
void prispa(T A) {
    priend(A, spa);
}
template <typename T, typename S>
bool printif(bool f, T A, S B) {
    if (f)
        print(A);
    else
        print(B);
    return f;
}

template <class... T>
void inp(T &...a) {
    (cin >> ... >> a);
}

} // namespace io
using namespace io;

// read graph
vector<vector<int>> read_edges(int n, int m, bool direct = false, int indexed = 1) {
    vector<vector<int>> edges(n, vector<int>());
    for (int i = 0; i < m; i++) {
        INT(u, v);
        u -= indexed;
        v -= indexed;
        edges[u].push_back(v);
        if (!direct) edges[v].push_back(u);
    }
    return edges;
}
vector<vector<int>> read_tree(int n, int indexed = 1) {
    return read_edges(n, n - 1, false, indexed);
}

template <typename T = long long>
vector<vector<pair<int, T>>> read_wedges(int n, int m, bool direct = false, int indexed = 1) {
    vector<vector<pair<int, T>>> edges(n, vector<pair<int, T>>());
    for (int i = 0; i < m; i++) {
        INT(u, v);
        T w;
        inp(w);
        u -= indexed;
        v -= indexed;
        edges[u].push_back({v, w});
        if (!direct) edges[v].push_back({u, w});
    }
    return edges;
}
template <typename T = long long>
vector<vector<pair<int, T>>> read_wtree(int n, int indexed = 1) {
    return read_wedges<T>(n, n - 1, false, indexed);
}

// yes / no
namespace yesno {

// yes
inline bool yes(bool f = true) {
    cout << (f ? "yes" : "no") << endl;
    return f;
}
inline bool Yes(bool f = true) {
    cout << (f ? "Yes" : "No") << endl;
    return f;
}
inline bool YES(bool f = true) {
    cout << (f ? "YES" : "NO") << endl;
    return f;
}

// no
inline bool no(bool f = true) {
    cout << (!f ? "yes" : "no") << endl;
    return f;
}
inline bool No(bool f = true) {
    cout << (!f ? "Yes" : "No") << endl;
    return f;
}
inline bool NO(bool f = true) {
    cout << (!f ? "YES" : "NO") << endl;
    return f;
}

// possible
inline bool possible(bool f = true) {
    cout << (f ? "possible" : "impossible") << endl;
    return f;
}
inline bool Possible(bool f = true) {
    cout << (f ? "Possible" : "Impossible") << endl;
    return f;
}
inline bool POSSIBLE(bool f = true) {
    cout << (f ? "POSSIBLE" : "IMPOSSIBLE") << endl;
    return f;
}

// impossible
inline bool impossible(bool f = true) {
    cout << (!f ? "possible" : "impossible") << endl;
    return f;
}
inline bool Impossible(bool f = true) {
    cout << (!f ? "Possible" : "Impossible") << endl;
    return f;
}
inline bool IMPOSSIBLE(bool f = true) {
    cout << (!f ? "POSSIBLE" : "IMPOSSIBLE") << endl;
    return f;
}

// Alice Bob
inline bool Alice(bool f = true) {
    cout << (f ? "Alice" : "Bob") << endl;
    return f;
}
inline bool Bob(bool f = true) {
    cout << (f ? "Bob" : "Alice") << endl;
    return f;
}

// Takahashi Aoki
inline bool Takahashi(bool f = true) {
    cout << (f ? "Takahashi" : "Aoki") << endl;
    return f;
}
inline bool Aoki(bool f = true) {
    cout << (f ? "Aoki" : "Takahashi") << endl;
    return f;
}

} // namespace yesno
using namespace yesno;

} // namespace templates
using namespace templates;

std::vector<int> Z_algorithm(const std::string &S) {
    int n = S.size();
    std::vector<int> Z(n);
    Z[0]  = n;
    int i = 1;
    int j = 0;
    while (i < n) {
        while (i + j < n && S[j] == S[i + j]) j++;
        Z[i] = j;
        if (j == 0) {
            i++;
            continue;
        }
        int k = 1;
        while (i + k < n && k + Z[k] < j) {
            Z[i + k] = Z[k];
            k++;
        }
        i += k;
        j -= k;
    }
    return Z;
}

template <typename T>
std::vector<int> Z_algorithm(const std::vector<T> &S) {
    int n = S.size();
    std::vector<int> Z(n);
    Z[0]  = n;
    int i = 1;
    int j = 0;
    while (i < n) {
        while (i + j < n && S[j] == S[i + j]) j++;
        Z[i] = j;
        if (j == 0) {
            i++;
            continue;
        }
        int k = 1;
        while (i + k < n && k + Z[k] < j) {
            Z[i + k] = Z[k];
            k++;
        }
        i += k;
        j -= k;
    }
    return Z;
}

void solve() {
    STRING(N);
    int le = len(N);
    INT(m);
    vec(int, A, le);
    fori(i, le) {
        A[i] = N[le - 1 - i] - '0';
    }

    auto add = [&](ll x, vector<int> &A) {
        int i = 0;
        while (x > 0) {
            if (i == len(A)) A.push_back(0);
            A[i] += x % 10;
            x /= 10;
            i++;
        }
        int j   = 0;
        int car = 0;
        while (1) {
            if (j == len(A)) A.push_back(0);
            A[j] += car;
            if (A[j] >= 10) {
                A[j] -= 10;
                car = 1;
            } else {
                car = 0;
            }
            j++;
            if (j >= i and car == 0) break;
        }
    };

    add(1, A);

    int x = 0;
    le    = len(A);
    fori(i, le - 1, -1, -1) {
        x = x * 10 + A[i];
        x %= m;
    }
    auto ans1 = A;
    add((m - x) % m, ans1);

    string S;
    fori(i, le) {
        S += A[i] + '0';
    }
    fori(12) S += '0';
    int lm = 0;
    {
        int mm = m;
        while (mm > 0) {
            S += mm % 10 + '0';
            mm /= 10;
            lm++;
        }
    }
    reverse(all(S));
    string T = to_string(m);

    auto Z  = Z_algorithm(S);
    int ma  = -1;
    char to = -1;
    int s   = -1;
    bool ok = false;
    vector<int> ss;
    fori(i, lm, len(Z) - lm + 1) {
        int z = min(Z[i], lm);
        if (z == lm) {
            if (chmax(ma, i + Z[i])) ok = true;
        } else {
            if (S[i + z] < T[z]) {
                if (chmax(ma, i + Z[i])) {
                    ss = {int(i)};
                    s  = i;
                    to = T[z];
                } else if (ma == i + Z[i] and to > T[z]) {
                    ss = {int(i)};
                    s  = i;
                    to = T[z];
                } else if (ma == i + Z[i] and to == T[z]) {
                    ss.push_back(i);
                }
            }
        }
    }

    if (ok) {
        S = S.substr(lm, len(S) - lm);
        reverse(all(S));
        while (S.back() == '0') S.pop_back();
        reverse(all(S));
        print(S);
        return;
    }

    string S1;
    fori(i, len(ans1) - 1, -1, -1) {
        S1 += ans1[i] + '0';
    }
    reverse(all(S1));
    while (S1.back() == '0') S1.pop_back();
    reverse(all(S1));

    auto S_ = S;
    for (auto s : ss) {
        S = S_;

        fori(i, s, len(S)) {
            int j = i - s;
            if (j < len(T))
                S[i] = T[j];
            else
                S[i] = '0';
        }
        S = S.substr(lm, len(S) - lm);
        reverse(all(S));
        while (S.back() == '0') S.pop_back();
        reverse(all(S));
        if (len(S) < len(S1)) {
            S1 = S;
        } else if (len(S) == len(S1) and S < S1) {
            S1 = S;
        }
    }
    print(S1);
}

int main() {
#ifndef INTERACTIVE
    std::cin.tie(0)->sync_with_stdio(0);
#endif
    // std::cout << std::fixed << std::setprecision(12);
    int t;
    t = 1;
    std::cin >> t;
    while (t--) solve();
    return 0;
}

// // #pragma GCC target("avx2")
// // #pragma GCC optimize("O3")
// // #pragma GCC optimize("unroll-loops")
// // #define INTERACTIVE
//
// #include "kyopro-cpp/template.hpp"
//
// #include "string/Z_algorithm.hpp"
//
// void solve() {
//     STRING(N);
//     int le = len(N);
//     INT(m);
//     vec(int, A, le);
//     fori(i, le) {
//         A[i] = N[le - 1 - i] - '0';
//     }
//
//     auto add = [&](ll x, vector<int> &A) {
//         int i = 0;
//         while (x > 0) {
//             if (i == len(A)) A.push_back(0);
//             A[i] += x % 10;
//             x /= 10;
//             i++;
//         }
//         int j   = 0;
//         int car = 0;
//         while (1) {
//             if (j == len(A)) A.push_back(0);
//             A[j] += car;
//             if (A[j] >= 10) {
//                 A[j] -= 10;
//                 car = 1;
//             } else {
//                 car = 0;
//             }
//             j++;
//             if (j >= i and car == 0) break;
//         }
//     };
//
//     add(1, A);
//
//     int x = 0;
//     le    = len(A);
//     fori(i, le - 1, -1, -1) {
//         x = x * 10 + A[i];
//         x %= m;
//     }
//     auto ans1 = A;
//     add((m - x) % m, ans1);
//
//     string S;
//     fori(i, le) {
//         S += A[i] + '0';
//     }
//     fori(12) S += '0';
//     int lm = 0;
//     {
//         int mm = m;
//         while (mm > 0) {
//             S += mm % 10 + '0';
//             mm /= 10;
//             lm++;
//         }
//     }
//     reverse(all(S));
//     string T = to_string(m);
//
//     auto Z  = Z_algorithm(S);
//     int ma  = -1;
//     char to = -1;
//     int s   = -1;
//     bool ok = false;
//     vector<int> ss;
//     fori(i, lm, len(Z) - lm + 1) {
//         int z = min(Z[i], lm);
//         if (z == lm) {
//             if (chmax(ma, i + Z[i])) ok = true;
//         } else {
//             if (S[i + z] < T[z]) {
//                 if (chmax(ma, i + Z[i])) {
//                     ss = {int(i)};
//                     s  = i;
//                     to = T[z];
//                 } else if (ma == i + Z[i] and to > T[z]) {
//                     ss = {int(i)};
//                     s  = i;
//                     to = T[z];
//                 } else if (ma == i + Z[i] and to == T[z]) {
//                     ss.push_back(i);
//                 }
//             }
//         }
//     }
//
//     if (ok) {
//         S = S.substr(lm, len(S) - lm);
//         reverse(all(S));
//         while (S.back() == '0') S.pop_back();
//         reverse(all(S));
//         print(S);
//         return;
//     }
//
//     string S1;
//     fori(i, len(ans1) - 1, -1, -1) {
//         S1 += ans1[i] + '0';
//     }
//     reverse(all(S1));
//     while (S1.back() == '0') S1.pop_back();
//     reverse(all(S1));
//
//     auto S_ = S;
//     for (auto s : ss) {
//         S = S_;
//
//         fori(i, s, len(S)) {
//             int j = i - s;
//             if (j < len(T))
//                 S[i] = T[j];
//             else
//                 S[i] = '0';
//         }
//         S = S.substr(lm, len(S) - lm);
//         reverse(all(S));
//         while (S.back() == '0') S.pop_back();
//         reverse(all(S));
//         if (len(S) < len(S1)) {
//             S1 = S;
//         } else if (len(S) == len(S1) and S < S1) {
//             S1 = S;
//         }
//     }
//     print(S1);
// }
//
// int main() {
// #ifndef INTERACTIVE
//     std::cin.tie(0)->sync_with_stdio(0);
// #endif
//     // std::cout << std::fixed << std::setprecision(12);
//     int t;
//     t = 1;
//     std::cin >> t;
//     while (t--) solve();
//     return 0;
// }

詳細信息

Test #1:

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

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: 0
Accepted
time: 66ms
memory: 3552kb

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
4811342000
3265496131
8294404062
2845521790
1114254674
6442013673
3744046867
3289624375
6477935360
1292587551
5504674689
2898829180
7882736025
2846033387
923...

result:

ok 100000 lines

Test #3:

score: 0
Accepted
time: 91ms
memory: 3628kb

input:

100000
81390699571930639918 18
48435143897560239761 20
51628960043353404809 75
47871552664477358704 12
59273263135375104780 37
76916933870890715781 71
23716799616473386311 99
68152894841119747794 73
87132912926681514765 23
14152130046962902029 76
46737628796812988809 24
40572731276115804589 44
26281...

output:

81390699571930639920
48435143897560239780
51628960043353404825
47871552664477358712
59273263135375104781
76916933870890715782
23716799616473386312
68152894841119747819
87132912926681514784
14152130046962902060
46737628796812988824
40572731276115804612
26281826064684565884
31440839508345860244
322404...

result:

ok 100000 lines

Test #4:

score: 0
Accepted
time: 60ms
memory: 3884kb

input:

3000
7455619882273084107817302538515753878442453229160411398764628307708900718973255504130291326067024207515690930909093270962092669445260372092520050302437090344417275699335988483584829739890137897388569114922455183012826259500289116227868472807384706143668392682061768042886347879057062217228560088...

output:

745561988227308410781730253851575387844245322916041139876462830770890071897325550413029132606702420751569093090909327096209266944526037209252005030243709034441727569933598848358482973989013789738856911492245518301282625950028911622786847280738470614366839268206176804288634787905706221722856008829738...

result:

ok 3000 lines

Test #5:

score: 0
Accepted
time: 47ms
memory: 4988kb

input:

30
744510720233756810275704474604569745531035132640480367036243214564743016462196305795005354253050610470663589619681077004760475741831076832118956334526511556189341001350810877421229216943948759000423729628987372479322027768925371301094347427331321252113757179047865411408129452046409100799804797802...

output:

744510720233756810275704474604569745531035132640480367036243214564743016462196305795005354253050610470663589619681077004760475741831076832118956334526511556189341001350810877421229216943948759000423729628987372479322027768925371301094347427331321252113757179047865411408129452046409100799804797802630...

result:

ok 30 lines

Test #6:

score: -100
Wrong Answer
time: 70ms
memory: 5104kb

input:

30
105809806761073271256887955844712146828376005200803410278562148742151831107043468856832007772397456498455138267388212588031778017994162605374749926516470689670500596864238694115641474868867283410369346289694466106606968664652794746652928273585660548259964572696052192486090603389036781439603103550...

output:

105809806761073271256887955844712146828376005200803410278562148742151831107043468856832007772397456498455138267388212588031778017994162605374749926516470689670500596864238694115641474868867283410369346289694466106606968664652794746652928273585660548259964572696052192486090603389036781439603103550889...

result:

wrong answer 3rd lines differ - expected: '189092935101233991739792297611...7261506413121041174357676231498', found: '189092935101233991739792297611...7261506413121041174357714626552'