QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#714157 | #5460. Sum of Numbers | qtoq# | WA | 244ms | 3852kb | C++17 | 3.3kb | 2024-11-05 21:55:23 | 2024-11-05 21:55:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef ONPC
#define deb(...) cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define deb(...)
#endif
// c++ short types
#define vt vector
//typedef long long ll;
typedef long double ld;
void whattime() { cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl; }
// const int mod = 1e9 + 7;
const int mod = 998244353;
const int inf = 1e9;
const int64_t infll = 1e13;
bool debug = false;
const ld eps = 1e-9;
const ld pi = acos(-1);
mt19937_64 rng((int) chrono::steady_clock::now().time_since_epoch().count());
struct num {
const int base = 1e9;
vt<int> elems;
num(int x = 0) {
elems = {x};
}
num(string &s, int l, int r) {
for(int i = r + 1; i > l; i -= 9) {
if(i < l + 9) {
elems.push_back(atoi(s.substr(l, i).c_str()));
} else {
elems.push_back(atoi(s.substr(i-9, 9).c_str()));
}
}
if(elems.empty()) {
elems = {0};
}
}
void add(num &x) {
int rem = 0;
for(int i = 0; i < max(elems.size(), x.elems.size()) || rem > 0; ++i) {
if(elems.size() == i) {
elems.push_back(i);
}
if(x.elems.size() > i) {
elems[i] += x.elems[i];
}
int tmp = elems[i] + rem;
elems[i] = tmp % base;
rem = tmp / base;
}
}
void print() {
for(auto x: elems) {
cout << x;
}
cout << '\n';
}
};
bool cmp(num &a, num &b) {
if(a.elems.size() != b.elems.size()) {
return a.elems.size() < b.elems.size();
}
for(int i = a.elems.size()-1; i>=0; --i) {
if(a.elems[i] != b.elems[i]){
return a.elems[i] < b.elems[i];
}
}
return true;
}
void solve() {
int n, k;
cin >> n >> k;
string s; cin >> s;
vt<int> vals;
int val = (n+k) / (k+1);
num res(s, 0, s.size()-1);
auto Dfs = [&](auto self, int id, int sum) -> void {
if(sum >= n || id >= k + 1) {
if(id == k + 1 and sum == n) {
num val(0);
int last = 0;
for(auto x: vals) {
assert(last + x - 1 < n);
num tmp = {s, last, last + x - 1};
val.add(tmp);
last += x;
}
if(not cmp(res, val)) {
swap(res.elems, val.elems);
}
}
return ;
}
for(auto z: {val-1,val,val+1}) if(z > 0) {
vals.push_back(z);
self(self, id + 1, sum + z);
vals.pop_back();
}
};
Dfs(Dfs, 0, 0);
res.print();
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int tt = 1;;
if(debug) {
tt = 1e3;
} else {
cin >> tt;
}
for(int t = 0; t < tt; ++t) {
solve();
}
#ifdef ONPC
whattime();
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3852kb
input:
2 8 1 45455151 2 1 42
output:
9696 6
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 244ms
memory: 3704kb
input:
10 1301 6 56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...
output:
70751969765381788293390454796442845640674313114201220940872147349467000278195132891492642370958372128548052759469542591687131751201574017716375336064325683696244924880363539009028256344 363208388440600322879333362495246780996933858794018327653859941043153715433593804305493817026686144990173122896996...
result:
wrong answer 1st lines differ - expected: '286183755510664079479706773787...6909797866802717925250679901255', found: '707519697653817882933904547964...3696244924880363539009028256344'