QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#521924 | #5460. Sum of Numbers | no_RED_no_DEAD | TL | 0ms | 3556kb | C++20 | 2.7kb | 2024-08-16 16:41:52 | 2024-08-16 16:41:53 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll N = 1e6 + 1;
const ll M = 1e9 + 7;
ll n, k, avg;
string res;
string s;
string add(string str1, string str2)
{
// Remove leading zeros from both strings
str1.erase(0, min(str1.find_first_not_of('0'), str1.size()-1));
str2.erase(0, min(str2.find_first_not_of('0'), str2.size()-1));
// If both strings become empty, return "0"
if (str1.empty() && str2.empty())
return "0";
// Before proceeding further, make sure length
// of str2 is larger.
if (str1.length() > str2.length())
swap(str1, str2);
// Take an empty string for storing result
string str = "";
// Calculate length of both string
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
// Initially take carry zero
int carry = 0;
// Traverse from end of both strings
for (int i=n1-1; i>=0; i--)
{
// Do school mathematics, compute sum of
// current digits and carry
int sum = ((str1[i]-'0') +
(str2[i+diff]-'0') +
carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
// Add remaining digits of str2[]
for (int i=n2-n1-1; i>=0; i--)
{
int sum = ((str2[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
// Add remaining carry
if (carry)
str.push_back(carry+'0');
// reverse resultant string
reverse(str.begin(), str.end());
return str;
}
string cmin(string &a, string &b) {
if (a.size() < b.size()) return a;
if (a.size() > b.size()) return b;
return (a < b) ? a : b;
}
void backtrack(ll pos, ll len, string sum, ll st, ll lastLen) {
if (pos > k) {
if (len != n) return;
res = cmin(res, sum);
return;
}
for (int i = max(avg - 10, max(1ll, lastLen - 1)); i <= min(avg + 10, lastLen + 1); i ++) {
ll npos = pos + 1, nlen = len + i, nst = st + i;
string nsum = sum;
if (nlen > n) break;
string t;
for (int j = st; j <= st + i - 1; j ++) t += s[j];
nsum = add(sum, t);
backtrack(npos, nlen, nsum, nst, i);
}
}
void doTest(ll testID) {
cin >> n >> k; k ++;
cin >> s; s = ' ' + s;
res = ""; for (int i = 1; i <= n; i ++) res += '9';
avg = n / k;
for (int i = max(1ll, avg - 1); i <= avg + 1; i ++) backtrack(1, 0, "0", 1, i);
cout << res << '\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int test = 1;
cin >> test;
for (int _ = 1; _ <= test; _ ++) doTest(test);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3556kb
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:
2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255 1330897896655974774035586406544907434842835048336411271110427836483063457950873824562288934364096546537492367401...