QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#138612#5460. Sum of Numbersammardab3an#WA 5ms3732kbC++171.7kb2023-08-12 01:41:522023-08-12 01:41:55

Judging History

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

  • [2023-08-12 01:41:55]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3732kb
  • [2023-08-12 01:41:52]
  • 提交

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;
    return std::min(a, b);
}

map<int, string> dp[10];

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;
    }
    
    auto it = dp[rem].find(i);
    if(it != dp[rem].end()) return it->second;
    
    int toDel = n/k;
    
    for(int can = toDel-2; can <= toDel+2; can++){
        if(can <= 0) continue;
        if(i+can > n) continue;
        string nxt = add(solve(i+can, n, k, rem-1, s), s.substr(i, can));
        res = min(res, nxt);
    }
    
    return dp[rem][i] = res;
}

int main(){

    int t;
    cin >> t;

    while(t--){


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

        for(int i = 0; i <= 6; i++) dp[i].clear();

        string s;
        cin >> s;

		if(n==0){
			n = 1e5;
			s = string(n, '1');
			k = 6;
		}
		
		n = s.size();
		
        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: 0ms
memory: 3512kb

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 5ms
memory: 3732kb

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:

2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255
2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735...

result:

wrong answer 2nd lines differ - expected: '133089789665597477403558640654...7219813869366061308627573151461', found: '286183755510664079479706773787...6909797866802717925250679901255'