QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189179 | #5460. Sum of Numbers | Zhou_JK | WA | 863ms | 3860kb | C++23 | 9.9kb | 2023-09-26 23:28:13 | 2023-09-26 23:28:15 |
Judging History
answer
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<sstream>
using namespace std;
class BigInteger
{
public :
//constructor
BigInteger(int = 0);
BigInteger(long long);
BigInteger(const string &);
BigInteger(const char *str)
{
*this = string(str);
}
//assignment operators
BigInteger &operator=(int num)
{
return *this = BigInteger(num);
}
BigInteger &operator=(long long num)
{
return *this = BigInteger(num);
}
BigInteger &operator=(const string &str)
{
return *this = BigInteger(str);
}
BigInteger &operator=(const char *str)
{
return *this = BigInteger(str);
}
//relational operators
bool operator<(const BigInteger &obj) const
{
return cmp(obj) < 0;
}
bool operator>(const BigInteger &obj) const
{
return cmp(obj) > 0;
}
bool operator<=(const BigInteger &obj) const
{
return cmp(obj) <= 0;
}
bool operator>=(const BigInteger &obj) const
{
return cmp(obj) >= 0;
}
bool operator==(const BigInteger &obj) const
{
return cmp(obj) == 0;
}
bool operator!=(const BigInteger &obj) const
{
return cmp(obj) != 0;
}
//arithmetic operators
BigInteger operator+() const
{
return *this;
}
BigInteger operator-() const
{
return BigInteger(-sign_, val_);
}
BigInteger operator+(const BigInteger &) const;
BigInteger operator-(const BigInteger &) const;
BigInteger operator*(const BigInteger &) const;
BigInteger operator/(const BigInteger &) const;
BigInteger operator%(const BigInteger &) const;
//compound assignment operators
BigInteger &operator+=(const BigInteger &obj)
{
return *this = *this + obj;
}
BigInteger &operator-=(const BigInteger &obj)
{
return *this = *this - obj;
}
BigInteger &operator*=(const BigInteger &obj)
{
return *this = *this * obj;
}
BigInteger &operator/=(const BigInteger &obj)
{
return *this = *this / obj;
}
BigInteger &operator%=(const BigInteger &obj)
{
return *this = *this % obj;
}
//increment and decrement operators
BigInteger &operator++()
{
return *this += 1;
}
BigInteger &operator--()
{
return *this -= 1;
}
BigInteger operator++(int);
BigInteger operator--(int);
//input and output
friend istream &operator>>(istream &, BigInteger &);
friend ostream &operator<<(ostream &, const BigInteger &);
protected :
enum div_type { division, remainder };
enum cmp_type { with_sign, without_sign };
static const int base_ = (int)1e8;
static const int width_ = 8;
BigInteger(int s, const vector<int> &v) : sign_(s), val_(v) {}
int cmp(const BigInteger &, cmp_type = with_sign) const;
BigInteger &delZero();
BigInteger &add(const BigInteger &);
BigInteger &sub(const BigInteger &);
BigInteger &mul(const BigInteger &, const BigInteger &);
BigInteger &div(BigInteger &, BigInteger, div_type = division);
private :
int sign_;
vector<int> val_;
};
BigInteger::BigInteger(int num) : sign_(0)
{
if (num < 0) sign_ = -1, num = -num;
else if (num > 0) sign_ = 1;
do
{
val_.push_back(num % base_);
num /= base_;
}
while (num);
}
BigInteger::BigInteger(long long num) : sign_(0)
{
if (num < 0) sign_ = -1, num = -num;
else if (num > 0) sign_ = 1;
do
{
val_.push_back(num % base_);
num /= base_;
}
while (num);
}
BigInteger::BigInteger(const string &str)
{
sign_ = str[0] == '-' ? -1 : 1;
int be = str[0] == '-' ? 1 : 0, en = str.size();
while ((en -= width_) >= be)
{
stringstream ss(str.substr(en, width_));
int temp;
ss >> temp;
val_.push_back(temp);
}
if ((en += width_) > be)
{
stringstream ss(str.substr(be, en - be));
int temp;
ss >> temp;
val_.push_back(temp);
}
delZero();
}
BigInteger BigInteger::operator+(const BigInteger &obj) const
{
if (sign_ * obj.sign_ == 1)
{
BigInteger temp;
return cmp(obj, without_sign) >= 0 ? (temp = *this).add(obj) : (temp = obj).add(*this);
}
else if (sign_ * obj.sign_ == -1) return *this - -obj;
else return sign_ == 0 ? obj : *this;
}
BigInteger BigInteger::operator-(const BigInteger &obj) const
{
if (sign_ * obj.sign_ == 1)
{
BigInteger temp;
return cmp(obj, without_sign) >= 0 ? (temp = *this).sub(obj) : (temp = -obj).sub(*this);
}
else if (sign_ * obj.sign_ == -1) return *this + -obj;
else return sign_ == 0 ? -obj : *this;
}
inline BigInteger BigInteger::operator*(const BigInteger &obj) const
{
BigInteger temp;
return (temp.sign_ = sign_ * obj.sign_) == 0 ? temp : temp.mul(*this, obj);
}
inline BigInteger BigInteger::operator/(const BigInteger &obj) const
{
BigInteger temp, mod = *this;
return cmp(obj, without_sign) < 0 || (temp.sign_ = sign_ * obj.sign_) == 0 ? temp : temp.div(mod, obj);
}
inline BigInteger BigInteger::operator%(const BigInteger &obj) const
{
BigInteger temp, mod = *this;
return cmp(obj, without_sign) < 0 || (temp.sign_ = sign_) == 0 ? mod : temp.div(mod, obj, remainder);
}
inline BigInteger BigInteger::operator++(int)
{
BigInteger temp = *this;
++*this;
return temp;
}
inline BigInteger BigInteger::operator--(int)
{
BigInteger temp = *this;
--*this;
return temp;
}
inline istream &operator>>(istream &in, BigInteger &obj)
{
string str;
if (in >> str) obj = str;
return in;
}
ostream &operator<<(ostream &out, const BigInteger &obj)
{
if (obj.sign_ == -1) out << '-';
out << obj.val_.back();
for (int i = obj.val_.size() - 2; i >= 0; i--)
out << setw(BigInteger::width_) << setfill('0') << obj.val_[i];
return out;
}
int BigInteger::cmp(const BigInteger &obj, cmp_type typ) const
{
if (typ == with_sign && sign_ != obj.sign_) return sign_ - obj.sign_;
int sign = typ == with_sign ? sign_ : 1;
if (val_.size() != obj.val_.size()) return sign * (val_.size() - obj.val_.size());
for (int i = val_.size() - 1; i >= 0; i--)
if (val_[i] != obj.val_[i]) return sign * (val_[i] - obj.val_[i]);
return 0;
}
inline BigInteger &BigInteger::delZero()
{
while((int)val_.size() > 1 && val_.back() == 0) val_.pop_back();
if (val_.empty() || val_.back() == 0) sign_ = 0;
return *this;
}
BigInteger &BigInteger::add(const BigInteger &obj)
{
int os = obj.val_.size();
val_.push_back(0);
for (int i = 0; i < os; i++)
{
long long tmp = (long long) val_[i] + obj.val_[i];
if(tmp >= base_) tmp -= base_, ++val_[i + 1];
val_[i] = tmp;
}
return delZero();
}
BigInteger &BigInteger::sub(const BigInteger &obj)
{
int pos = obj.val_.size();
for (int i = 0; i < pos; i++)
{
long long tmp = (long long) val_[i] - obj.val_[i];
if(tmp < 0) tmp += base_, --val_[i + 1];
val_[i] = tmp;
}
while (val_[pos] < 0) val_[pos] += base_, --val_[++pos];
return delZero();
}
BigInteger &BigInteger::mul(const BigInteger &a, const BigInteger &b)
{
int as = a.val_.size(), bs = b.val_.size();
val_.resize(as + bs);
for (int i = 0; i < as; i++) for (int j = 0; j < bs; j++)
{
int x = i + j;
long long tmp = val_[x] + (long long) a.val_[i] * b.val_[j];
val_[x + 1] += tmp / base_;
tmp %= base_;
val_[x] = tmp;
}
return delZero();
}
BigInteger &BigInteger::div(BigInteger &a, BigInteger b, div_type typ)
{
int move = a.val_.size() - b.val_.size();
val_.resize(move + 1);
b.val_.insert(b.val_.begin(), move, 0);
for (int i = move; i >= 0; i--)
{
int left = 0, right = base_;
while (left + 1 < right)
{
int mid = (left + right) >> 1;
if (a.cmp(b * BigInteger(mid), without_sign) >= 0) left = mid;
else right = mid;
}
val_[i] = left;
a.sub(b * BigInteger(left));
b.val_.erase(b.val_.begin());
}
return typ == division ? delZero() : a;
}
int n,k;
string s;
int len,ret;
BigInteger ans;
void dfs(int step,int sv,BigInteger sum)
{
if(ret-sv<-(k-step+1)||ret-sv>(k-step+1)) return;
// cerr<<"now"<<sv<<" "<<sum<<"\n";
if(step==k+1)
{
if(ans==-1||sum<ans) ans=sum;
return;
}
for(int v=-1;v<=1;v++)
{
if(ret-sv-v<-(k-step)||ret-sv-v>(k-step)) continue;
// cerr<<"divide"<<step<<" "<<v<<" "<<s.substr(len*step+sv,len+v)<<"\n";
dfs(step+1,sv+v,sum+s.substr(len*step+sv,len+v));
}
return;
}
void solve()
{
cin>>n>>k;
cin>>s;
ans=-1;
len=(n+k)/(k+1),ret=n-len*(k+1);
dfs(0,0,0);
// len=n/(k+1),ret=n-len*(k+1);
// dfs(0,0,0);
cout<<ans<<"\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int T;
cin>>T;
while(T--)
solve();
return 0;
}
/*
1
16 4
7737414833519746
388+645+83+487+119+925+699
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3536kb
input:
2 8 1 45455151 2 1 42
output:
9696 6
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 863ms
memory: 3740kb
input:
10 1301 6 56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...
output:
2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255 1330897896655974774035586406544907434842835048336411271110427836483063457950873824562288934364096546537492367401...
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 24ms
memory: 3688kb
input:
3 68312 1 97721793232462295468345161395388245623318759168492992575579675893432997968276399269361852215552871434587866112799189725614696256526239439933158479577223186954475137216722241699568491426254779997896288355965839555276546655174417599498493234323661877664997543475525992228661739933395881544363...
output:
165361439370989969611281150911906045608758081895436745302721903170895741831200164992289491213814537683869854746077326282091529052307364873668669947061960472772133388934416763345394409983809105665214082485429685401199520114459892594221344388768178495952936142426649338494017013932243993161711816767032...
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 24ms
memory: 3708kb
input:
3 104062 2 6586987777965872861989232258194733262765989634859654379877935579528781688159322513128986886481716832258115626748535521126479244461411522889744479186513298679746684345233214226141199846787117661159318966767135455745184384324833645134232699525177668629239933843658643667417337451334591594819...
output:
536246497591460214899497073157707308719964754106858053886979792537707870310253164299747153718261114131636990130350872742851787818427135113617679220183754863021102973854172956224432906133335696002890140207617299467927865340000078212572980076032917286214327823207585568582552662612028254042515906063023...
result:
ok 3 lines
Test #5:
score: 0
Accepted
time: 75ms
memory: 3756kb
input:
3 139639 3 6796297676823528648589397343663999328612414278384367347213637689464348185237425534836886677731351726963453579377613988437426483671299519186839344132333793569544718489728294346989818592279444871423953477919473799463194216678119582972131632322347549538925164854516141164554772823372476647126...
output:
126364763403906564661708542497996384758493064461499142133308596067494936258655259133526434841726196274703766844552726915088706942994746309192813887599486034682622494344506129054929148805249803660505979746418821689885230718372401950523582639142168746487771785553740446005221635828358569536474437471762...
result:
ok 3 lines
Test #6:
score: 0
Accepted
time: 58ms
memory: 3676kb
input:
3 74882 4 34214651312364628656844245717387533432968776973534628295922991352118459564756246556257779312918116552177726519173345828839532314119892538422953191411989526818837499574837984599131437923829691932871927272598159916936847255554115731624229194141184295327556843698221845942155322267254877729895...
output:
652879872016718790343280470756061301445687203441680798826476501917145925778781111109343489062160438478188515389826291555084101569285971285993846457561557667210690487169933469816410296697081463378678731150633158904900018680809858404118923436246875692603950100371337341615443319224405626522868813204392...
result:
ok 3 lines
Test #7:
score: 0
Accepted
time: 328ms
memory: 3860kb
input:
3 191014 5 4625226525489118654543959629348153444699986242632464847423452621546972849227817733763237372239335981427166568676892839618898764121637356149689834276433852165285729163339388526992792556894792278543347578452353389831593643818698478199319882191378795871195868331188637941453446682259469316457...
output:
183433830723962716661960230479245454318025962759914611110498608412928382555357890844482989235397326332530009244441167586569039863115637789245293728629736732433450983262080427109116060046615860768206472546822403232142108561871987242547562597599614404577271199268661821460539766659678709433633532926618...
result:
ok 3 lines
Test #8:
score: 0
Accepted
time: 650ms
memory: 3664kb
input:
3 89585 6 94845874282137954862385693488638122989575112616926699152984211114725742468459969145259337775618857796224956393949274481244989438351268853962294993267143851959948416128442795399969913525879669463929768397987966315181534158658746641933973131881592663842895938779816214825692812493134824556695...
output:
299751945436714405327965382630641151220078087436306886008455290218867774598033154703298350470955635925864103042961309005090519862571207481236797905629359014249368432851738777659221783920211895526401428737190140639519448861544949137222152380658013492841793431645174714140875875011098436373161645837902...
result:
ok 3 lines
Test #9:
score: 0
Accepted
time: 23ms
memory: 3512kb
input:
1000 438 1 2517849689325865499132114642336162462526229452777943938182992956383698881134842963395515712949251433652761697475196628342214595685672144112568956575717136898856177694197933946755524666375993512634793331951161492287212454971511815257823292342711659258415167759313776646368654965574417395231...
output:
748936150458368879147482630159457763028554322942431259314856737377893084493327951133306407733863814927410043574237076571833145300484875759606530375804038146070837054761731126648297282834150805998661305332252023826677983 91742211464580719040769832120828477315449383443456839582655398455396278130875002...
result:
ok 1000 lines
Test #10:
score: 0
Accepted
time: 42ms
memory: 3556kb
input:
1000 84 2 966932289765425941985736948627128913324775123274122997916444286764739984266379596918 70 2 5331446293763416152151916485841778781581747798477137375944877329687691 9 2 634624591 814 2 1988645967814683323112433424295394438122288481686898542983777796753686147185684924621213554165142457245886757...
output:
18826500994871756013649266195 295310256670327308687194 1849 284825795169730623800202989098038033639343406140577208629162918151307022180084222616119982223024776344842017851725895275795084612484054272282348654966167150019465644108270898181205721199473080438042040896310370198851400852136186082174700276...
result:
ok 1000 lines
Test #11:
score: -100
Wrong Answer
time: 105ms
memory: 3492kb
input:
1000 339 3 9163225412581255315266828923167711137296521138867213374363794548283572234447976575396136427848756366754713547897594499817281488658226795963345311956629297357965219254498864379562223818165453699818569782568512899896568369453633434563347531526352315344759793546252139786416256297532237728985...
output:
17320085336505381033402042979863864748728993071457871710902369621896971390566049928087 303414409729053513885481240687 17597270 15669 1283838053980036200051841389000892463151272030330831803493589 1560322045162299829921416791329484329527 4243531807036148642992251191455715323698841515530953264341379464...
result:
wrong answer 647th lines differ - expected: '801917738558266299047286340489553451866874918044288166', found: '807113230002867849893222958937345670023730099119700733'