QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#682192 | #5460. Sum of Numbers | ocharin | WA | 29ms | 3992kb | C++20 | 2.9kb | 2024-10-27 14:21:19 | 2024-10-27 14:21:20 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct BigInt {
static const int MOD = 1000000000;
static const int N_BIT = 9;
vector<int> nums;
BigInt(const string s, int n) {
int val = 0, base = 1;
for(int i = n - 1; i >= 0; i--) {
val += (s[i] - '0') * base;
base *= 10;
if(base == MOD) {
nums.push_back(val);
val = 0;
base = 1;
}
}
if(nums.empty() || val != 0) {
nums.push_back(val);
}
}
BigInt &operator= (const BigInt &&u) {
nums = move(u.nums);
return *this;
}
BigInt &operator+= (const BigInt &u) {
nums.resize(max(nums.size(), u.nums.size()));
int carry = 0;
for(size_t i = 0; i < nums.size(); i++) {
nums[i] += carry + (i < u.nums.size() ? u.nums[i] : 0);
if(nums[i] >= MOD) {
nums[i] -= MOD;
carry = 1;
} else {
carry = 0;
}
}
if(carry) {
nums.push_back(carry);
}
return *this;
}
bool operator< (const BigInt &u) {
if(nums.size() != u.nums.size()) {
return nums.size() < u.nums.size();
}
for(size_t i = nums.size() - 1; i < nums.size(); i--) {
if(nums[i] != u.nums[i]) {
return nums[i] < u.nums[i];
}
}
return false;
}
};
ostream& operator<< (ostream &os, const BigInt &u) {
ios states(nullptr);
states.copyfmt(os);
os << u.nums.back();
for(int i = (int)u.nums.size() - 2; i >= 0; i--) {
os << setw(BigInt::N_BIT) << setfill('0') << u.nums[i];
}
os.copyfmt(states);
return os;
}
void solve(){
int n,m;
string s;
cin>>n>>m>>s;
BigInt res(s,n);
vector<int>d(m+1);
auto dfs=[&](auto dfs,int id)->void{
if(id>m){
int sum=0;
auto dx=d;
for(int i=1;i<=m;++i) dx[i]+=dx[i-1],sum+=dx[i];
sum=n-sum;
if(sum%(m+1)) return;
sum/=(m+1);
dx[0]=sum;
for(int i=1;i<=m;++i) dx[i]+=dx[i-1];
if(*min_element(dx.begin(),dx.end())<=0) return;
// for(auto x:dx) cerr<<x<<" ";
// cerr<<endl;
BigInt ans("",0);
int lst=0;
for(int i=0;i<=m;++i){
string t=s.substr(lst,dx[i]);
ans+=BigInt(t,dx[i]);
lst+=dx[i];
}
if(ans<res) res=move(ans);
return;
}
for(int i=-1;i<=1;++i){
d[id]=i;
dfs(dfs,id+1);
}
};
dfs(dfs,1);
cout<<res<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3480kb
input:
2 8 1 45455151 2 1 42
output:
9696 6
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 29ms
memory: 3992kb
input:
10 1301 6 56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...
output:
649607663999112818695535121091271716883103814111526466712058737770955337281586527310775750362280871235076286553850215074801795398406281732371434065121612271001585604761636132649176910707 98449253467064516975946051370424824777341776688849859608679977471459596394177947451956361145217999464679282143853...
result:
wrong answer 1st lines differ - expected: '286183755510664079479706773787...6909797866802717925250679901255', found: '649607663999112818695535121091...1001585604761636132649176910707'