QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#356948#5460. Sum of Numbersucup-team1001RE 1ms3828kbC++173.9kb2024-03-18 16:24:432024-03-18 16:24:43

Judging History

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

  • [2024-03-18 16:24:43]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3828kb
  • [2024-03-18 16:24:43]
  • 提交

answer

/*

Author: Haze

2024/3/17

*/
#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define irep(i, l, r) for(int i = (l); i <= (r); ++ i)
#define drep(i, r, l) for(int i = (r); i >= (l); -- i)
#define IOS ios::sync_with_stdio(false), cin.tie(nullptr);
using namespace std;
typedef long long ll;

inline ll read() {
    ll s = 0;
    bool fl = false;
    char ch = (char) getchar();
    while (!isdigit(ch)) {
        if (ch == '-')fl = true;
        ch = (char) getchar();
    }
    while (isdigit(ch)) {
        s = s * 10 + (ch ^ 48);
        ch = (char) getchar();
    }
    return fl ? -s : s;
}

constexpr int P = 1E9;
struct BigInt {
    std::vector<int> v;
};

void add(BigInt &a, BigInt &b) {
    if (a.v.size() < b.v.size())swap(a, b);
    a.v.resize(a.v.size(), +1);
    for (int i = 0; i < b.v.size(); i++) {
        a.v[i] += b.v[i];
    }
    for (int i = 0; i < a.v.size(); i++) {
        if (a.v[i] >= 10) {
            a.v[i] -= 10;
            a.v[i + 1]++;
        }
    }
    if (a.v.back() == 0) {
        a.v.pop_back();
    }
}

bool operator<(const BigInt &a, const BigInt &b) {
    if (a.v.size() != b.v.size()) {
        return a.v.size() < b.v.size();
    }
    for (int i = int(a.v.size()) - 1; i >= 0; i--) {
        if (a.v[i] != b.v[i]) {
            return a.v[i] < b.v[i];
        }
    }
    return false;
}

void print(const BigInt &a) {
    const auto &v = a.v;
    if (v.empty()) {
        std::cout << "0\n";
        return;
    }
    std::cout << v.back();
    for (int i = int(v.size()) - 2; i >= 0; i--) {
        cout << v[i];
    }
    std::cout << "\n";
}

const int mod = 1000000000 + 7;
const int itinf = 1000000999;
const ll llinf = 2e18;
const int N = 500099;
int del[20];
string s;
int n, k, fl;
BigInt ANS;
ll ans;

void dfs(int x) {
    if (x == k) {
        if ((n - accumulate(del, del + 20, 0)) % k == 0) {
            vector<int> dig(k);
            int D = n - accumulate(del, del + 20, 0);
            D /= k;
            irep(i, 0, k - 1) {
                dig[i] = D + del[i];
                if (dig[i] == 0)return;
            }
            BigInt A;
            ll a = 0;
            int p = 0;
            if (n / k <= 16) {
                irep(i, 0, k - 1) {
                    ll B = 0, base = 1;
                    while (p < n && dig[i]) {
                        B += (s[p] - '0') * base;
                        base *= 10;
                        ++p, --dig[i];
                    }
                    a += B;
                }
                if (!fl)fl = true, ans = a;
                else ans = min(ans, a);
                return;
            }
            irep(i, 0, k - 1) {
                BigInt B;
                B.v.resize(dig[i]);
                int tot = 0;
                while (p < n && dig[i]) {
                    B.v[tot++] = s[p] - '0';
                    ++p;
                    --dig[i];
                }
                add(A, B);
                if (fl == 1 && (A.v.size() > ANS.v.size() || A.v.size() == ANS.v.size() && A.v.back() > ANS.v.back()))
                    return;
            }
            if (!fl)fl = true, ANS = A;
            else if (A < ANS)ANS = A;
        }

        return;
    }
    del[x] = -1;
    dfs(x + 1);
    del[x] = 0;
    dfs(x + 1);
    del[x] = 1;
    dfs(x + 1);
};

void solve() {
//    n, k;
    cin >> n >> k;
    cin >> s;
    memset(del, 0, sizeof del);
    ++k;
    reverse(s.begin(), s.end());
    fl = false;
    dfs(0);
    if (n / k <= 16)cout << ans << '\n';
    else
        print(ANS);
    ANS.v.clear();
}

int main() {
    IOS
    int T;

    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
/*
 * 1299782424427
37
13525
20803460463
334823
29219
1225424
16624
204
704559366
29219
1225424
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3828kb

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Runtime Error

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:


result: