QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#521942 | #5460. Sum of Numbers | no_RED_no_DEAD | WA | 0ms | 3640kb | C++20 | 2.8kb | 2024-08-16 16:47:05 | 2024-08-16 16:47:05 |
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 - 1, max(1ll, lastLen - 1)); i <= min(avg + 2, lastLen + 1); i ++) {
ll npos = pos + 1, nlen = len + i, nst = st + i;
string nsum = sum;
if (nlen > n) break;
if (len + (n - pos + 1) * (avg - 1) > n) break;
if (len + (n - pos + 1) * (avg + 2) < n) continue;
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: 0
Wrong Answer
time: 0ms
memory: 3640kb
input:
2 8 1 45455151 2 1 42
output:
99999999 6
result:
wrong answer 1st lines differ - expected: '9696', found: '99999999'