QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#138601#5460. Sum of Numbersammardab3an#TL 1ms3476kbC++171.5kb2023-08-12 01:23:392023-08-12 01:24:55

Judging History

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

  • [2023-08-12 01:24:55]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3476kb
  • [2023-08-12 01:23:39]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

string add(string a, string b){
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    string res = "";
    int n = a.length();
    int m = b.length();
    int carry = 0;
    for(int i = 0; i < max(n, m); i++){
        int f = 0, s = 0;
        if(i < n) f = a[i]-'0';
        if(i < m) s = b[i]-'0';
        carry += f + s;
        res += '0'+carry%10;
        carry /= 10;
    }
    if(carry) res += '0'+carry%10;
    reverse(res.begin(), res.end());
    return res;
}

string min(string a, string b){
    if(a.length() < b.length()) return a;
    if(a.length() > b.length()) return b;
    int n = a.length();
    for(int i = 0; i < n; i++){
        if(a[i] < b[i]) return a;
        if(a[i] > b[i]) return b;
    }
    return a;
}

string solve(int i, int n, int k, int rem, string &s){
    string res(n, '9');
    if(rem < 0) return res;
    if(i == n){
        if(rem == 0) return "0";
        return res;
    }
    int toDel = n/k;
    for(int can = toDel-3; can <= toDel+3; can++){
        if(can <= 0) continue;
        if(i+can > n) continue;
        res = min(res, add(solve(i+can, n, k, rem-1, s), s.substr(i, can)));
    }
    return res;
}

int main(){

    int t;
    cin >> t;

    while(t--){

        int n, k;
        cin >> n >> k;

        string s;
        cin >> s;

        cout << solve(0, n, k+1, k+1, s) << "\n";

    }

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:


result: