#include <iostream> #include <cstdio> using namespace std; class Carrier; class BigInt; class Decimal; class Carrier { friend class Decimal; protected: struct Four { Four *upper; Four *under; unsigned long value; Four(); }; Four *most; Four *least; unsigned long count; Carrier(); ~Carrier(); virtual unsigned long calcFlow(const unsigned long& value) const = 0; virtual unsigned long calcUnit(const unsigned long& value) const = 0; void init(unsigned long value); Four* carry(); Carrier& copy(const Carrier& carrier); Carrier& copy(const Carrier *carrier); Carrier& swap(Carrier& carrier); Carrier& add(const Carrier& value); Carrier& mul(const Carrier& value, Carrier& temp); }; class BigInt : public Carrier { friend class Decimal; private: static const unsigned long UNIT = 0xFFFF; static const unsigned long SHIFT = 16; unsigned long calcFlow(const unsigned long& value) const; unsigned long calcUnit(const unsigned long& value) const; public: BigInt(); BigInt(unsigned long value); BigInt(const BigInt& value); BigInt(const BigInt *value); BigInt& operator ++ (); BigInt& operator += (const BigInt& value); BigInt operator + (const BigInt& value); BigInt& operator *= (const BigInt& value); BigInt operator * (const BigInt& value); BigInt& operator = (const BigInt& value); unsigned long digits() const; void print() const; void println() const; }; class Decimal : public Carrier { private: static const unsigned long UNIT = 10000; unsigned long calcFlow(const unsigned long& value) const; unsigned long calcUnit(const unsigned long& value) const; public: Decimal(); Decimal(unsigned long value); Decimal(const Decimal& value); Decimal(const Decimal *value); Decimal(const BigInt& value); Decimal(const BigInt *value); Decimal& operator ++ (); Decimal& operator = (const Decimal& value); Decimal& operator += (const Decimal& value); Decimal& operator *= (const Decimal& value); Decimal operator + (const Decimal& value); Decimal operator * (const Decimal& value); unsigned long digits() const; void print() const; void println() const; }; Carrier::Four::Four() : upper(NULL), under(NULL), value(0) {} Carrier::Carrier() : count(1) { most = least = new Four; } Carrier::~Carrier() { Four *temp; while (most != NULL) { temp = most->under; delete most; most = temp; } //count = 0; //most = least = NULL; } void Carrier::init(unsigned long value) { Four *temp = least; while (value > 0) { if (temp == NULL) { temp = carry(); } temp->value = calcUnit(value); value = calcFlow(value); temp = temp->upper; } } Carrier& Carrier::copy(const Carrier& carrier) { Four *me = least; Four *he = carrier.least; while (he != NULL) { if (me == NULL) { me = carry(); } me->value = he->value; me = me->upper; he = he->upper; } while (me != NULL) { me->value = 0; me = me->upper; } return *this; } Carrier& Carrier::copy(const Carrier *carrier) { if (carrier != NULL) { copy(*carrier); } return *this; } Carrier& Carrier::swap(Carrier& carrier) { Four *temp_most = most; Four *temp_least = least; int temp_count = count; most = carrier.most; least = carrier.least; count = carrier.count; carrier.most = temp_most; carrier.least = temp_least; carrier.count = temp_count; return *this; } Carrier::Four* Carrier::carry() { count++; Four *temp = new Four; temp->under = most; most->upper = temp; most = temp; return most; } Carrier& Carrier::add(const Carrier& value) { unsigned long flow = 0; Four *me = least; Four *he = value.least; while (he != NULL) { if (me == NULL) { me = carry(); } me->value += he->value + flow; flow = calcFlow(me->value); me->value = calcUnit(me->value); me = me->upper; he = he->upper; } while (flow > 0) { if (me == NULL) { me = carry(); } me->value += flow; flow = calcFlow(me->value); me->value = calcUnit(me->value); me = me->upper; } return *this; } Carrier& Carrier::mul(const Carrier& value, Carrier& temp) { swap(temp); Four *me = least; Four *he = value.least; Four *she, *who; unsigned long flow1, flow2, product; while (he != NULL) { if (me == NULL) { me = carry(); } she = temp.least; who = me; flow1 = flow2 = 0; while (she != NULL) { if (who == NULL) { who = carry(); } product = he->value * she->value; flow2 = calcFlow(product); who->value += calcUnit(product); flow2 += calcFlow(who->value); who->value = calcUnit(who->value); who->value += flow1; flow1 = flow2 + calcFlow(who->value); who->value = calcUnit(who->value); who = who->upper; she = she->upper; } while (flow1 > 0) { if (who == NULL) { who = carry(); } who->value += flow1; flow1 = calcFlow(who->value); who->value = calcUnit(who->value); who = who->upper; } me = me->upper; he = he->upper; } return *this; } unsigned long BigInt::calcFlow(const unsigned long& value) const { return value >> SHIFT; } unsigned long BigInt::calcUnit(const unsigned long& value) const { return value & UNIT; } BigInt::BigInt() {} BigInt::BigInt(unsigned long value) { init(value); } BigInt::BigInt(const BigInt& value) { copy(value); } BigInt::BigInt(const BigInt *value) { copy(value); } void BigInt::print() const { Decimal temp(*this); temp.print(); } void BigInt::println() const { Decimal temp(*this); temp.println(); } unsigned long BigInt::digits() const { Decimal temp(*this); return temp.digits(); } BigInt& BigInt::operator ++ () { add(BigInt(1)); return *this; } BigInt& BigInt::operator += (const BigInt& value) { add(value); return *this; } BigInt BigInt::operator + (const BigInt& value) { BigInt sum(value); sum.add(*this); return sum; } BigInt& BigInt::operator *= (const BigInt& value) { BigInt zero; mul(value, zero); return *this; } BigInt BigInt::operator * (const BigInt& value) { BigInt product(value), zero; product.mul(*this, zero); return product; } BigInt& BigInt::operator = (const BigInt& value) { copy(value); return *this; } unsigned long Decimal::calcFlow(const unsigned long& value) const { return value / UNIT; } unsigned long Decimal::calcUnit(const unsigned long& value) const { return value % UNIT; } Decimal::Decimal() {} Decimal::Decimal(unsigned long value) { init(value); } Decimal::Decimal(const Decimal& value) { copy(value); } Decimal::Decimal(const Decimal *value) { copy(*value); } Decimal::Decimal(const BigInt& value) { const Decimal temp(BigInt::UNIT + 1); Four *cur = value.most; while (cur != NULL) { Decimal zero; mul(temp, zero); add(Decimal(cur->value)); cur = cur->under; } } Decimal::Decimal(const BigInt *value) { if (value != NULL) { Decimal temp(*value); swap(temp); } } void Decimal::print() const { Four *temp = most; int f = 0; while (temp != NULL) { if (f == 0) { if (temp->value != 0) { printf("%ld", temp->value); f = 1; } } else { printf("%04ld", temp->value); } temp = temp->under; } if (f == 0) { putchar('0'); } } void Decimal::println() const { print(); putchar('\n'); } unsigned long Decimal::digits() const { unsigned long temp_count = count; Four *temp = most; while (temp->value == 0) { temp_count--; if ((temp = temp->under) == NULL) { return 1; } } temp_count *= 4; unsigned long unit = UNIT / 10; while (temp->value < unit) { temp_count--; unit /= 10; } return temp_count; } Decimal& Decimal::operator ++ () { return add(Decimal(1)), *this; } Decimal& Decimal::operator += (const Decimal& value) { add(value); return *this; } Decimal Decimal::operator + (const Decimal& value) { Decimal sum(value); sum.add(*this); return sum; } Decimal& Decimal::operator *= (const Decimal& value) { Decimal zero; mul(value, zero); return *this; } Decimal Decimal::operator * (const Decimal& value) { Decimal product(value), zero; product.mul(*this, zero); return product; } Decimal& Decimal::operator = (const Decimal& value) { copy(value); return *this; } int main() { BigInt val1(111111); BigInt val2(val1 + 800800); BigInt val3(val1 * val2); BigInt val4(val1 + val2); val3 *= val1 * val1; val4 += val2 * val2;; val1.println(); cout << "digits: " << val1.digits() << endl; val2.println(); cout << "digits: " << val2.digits() << endl; val3.println(); cout << "digits: " << val3.digits() << endl; val4.println(); cout << "digits: " << val4.digits() << endl; Decimal dec1(111111); Decimal dec2(dec1 + 800800); Decimal dec3(dec1 * dec2); Decimal dec4(dec1 + dec2); dec3 *= dec1 * dec1; dec4 += dec2 * dec2; dec1.println(); cout << "digits: " << dec1.digits() << endl; dec2.println(); cout << "digits: " << dec2.digits() << endl; dec3.println(); cout << "digits: " << dec3.digits() << endl; dec4.println(); cout << "digits: " << dec4.digits() << endl; BigInt val5(val4 * val4 * val4); val5 *= val5 * val5 * val5; val5 *= val5 * val5 * val5; val5 *= val5 * val5 * val5; val5 *= val5 * val5 * val5; Decimal dec5(val5); dec5.println(); cout << "digits: " << dec5.digits() << endl; return 0; }
Standard input is empty
111111 digits: 6 911911 digits: 6 1250902968819939275841 digits: 22 831582694943 digits: 12 111111 digits: 6 911911 digits: 6 1250902968819939275841 digits: 22 831582694943 digits: 12 30717110586865307471753083662808277959924674823911951926535126594019877205272322331563991241435006268595788547402201518280142955097787319821132324268627443444916938120548685196201002869551946857477316775393414316845441211301705003019264297666434282990442642561485908524426616314897116483456444402685150850922774311019374271036375297398591611462188845082945666420464599252792431260078424294803824245622910915188470020458017538362185220908836183366295910954530043288935375123100172450890511060281777753913237871686170742073625029816515402173292015603123546160594178190551121797159013215468899352033337529920937579290855459323279797683228510666372056018863669496882742417053260671121092463937420744164356768534447924810513924282949552041190909590585415510129163662767143629728388187619520130714748074803931465797414868919571382679576126977962942834969754960614129316928611271942438230666541636453578612779547492748020072744552092984648916781231210303528826734129381278395505941665675551125168936100388513667323090677935472449245144503063222977596519653702406451153578690963401999044572807042154629787036841869173510518463881983532026083701988255417145368735303929576633421658105157217600975704066093455564193479641986911944136558567865276852401183270501191621699569910503826238390122111240346631245856049451908092313484753136282644757248313101980517231722543855514051938174868080478216077236450682788788283628562994768448263060875779616716217155758101712697448501586528142673273081930186157394978311262183530471519240359123702657494883262290714871708384883987674038658958996281193441706523583894075220959527943111235199675499935200030380261378525779941781637569954140531976021484226014588938952316887148773804585439922821531921113167785457089889521726175158797412187802908376516688596026699380319783849961032311760256951536371513980896125985949503539306391897076879968625687343235938547745816660079052650418348187418084881218642048027144809502988248488360561288976728644148113776892460036323518131580010633162888650929288710994678286898619718304573469335782107617348147732997478743650963561804486551629987068276849451456744265228019246668307669807183821820760325305126785719962894577155778404497193026601219205538603993487154010770565683538498044306782807450203583109114025526285694532163602791355462768567613002664917486081994361453401174050706483789603942752860914487942168888114471121420671705988810539472408012632880766746035983233728592117793783527190574190113758266420086761926588380227552037200761101515755333735524474303673504080698183152990140075636229127508119574553043400949137537824594098423158893839060919191496599367516574322910329666967038538606311498961443857983683831093329816721928088284771207010450425679939328904140977795708823959043719401776617933848733906503154934563612532903392734635529971018903258606874199295918761618565811750255321216861461543034052183913103596135277843798832415584513667537862553305361363829113035243202658448290297406557895749474717519555784879653989873883123620958469541909895105944207292505702043229764021840897796830564261418826277570032727158149885132371509927005396295677635180845351097153219473157295576694919493216816270230009261976356125102386249631729380767617277586430898775849003601602218717384905995446981905241324264308560565318362644175247605638212974071165049822943981052158336433335629946324269822109045685937362006403146131124368953647038599425005009177074155398796989842248692031831706677341369255924461531377734484233553522172307928586550446457461724937935892932982980487068362689702908542001750013936414199274740224364805642094531806131922254513237047541632739019220805012499214359286502779262059914252453059979056472248363271945716603966662713420066949275905219536672639511524289082484123690203488450740707999850720039366944294625549414620541865780955624623882311966511659577781657579931065312229896208651196889708605838220620645992396786951500368865273271040463991837692174156080876007424458655064024902544744453503012281940395748164147755034279644656310269600348573205192323324355295197635582712091519114027598063990985005960589672630371273810029319130415379258998481051054016260433383191610621213496335415981357237525474313301195008372713518103030835493412084983179453672216693103839827260203296437328404900325029521960779166763043307276062018777260722606213876494154909786904595114704078091448455443252165577977873866028028438105316522810390948045653790576692423027339016728708611445761797589394782598118812589710012819653096002494429816026731901526109350626475083889422225062261930488925859838093362834877705186197772949571333951923465503587720123608732660889621433200900309329347340673319564414871893931681805700179729831624377542339054705021510946264141932267988160437882522818508409431359058220747074976451627747269333557089651696478660633954294442080445760384112991586515660566643155711278512609882984866898563995826282219984045344648291517934320113597932055028773026299403859176500115772250349980374095180292862618515723019353819558789061132898896652966712318908786171178709043855823564815494883078766895917512879471245197164849760316679985652037412355194983336252501735231577664857050575562231154132495184334975133370549660315552742932780145724634086274365253546847920395740968531747802301228337660317994036060767632410754683205657610517114566392328414573849665703051316334409114029974956774345527161830037069877669921147129310681789953632165612710641590007808398834950813497684480320364210524416618737488173518112016778157301449797944267441917456089135679348298171856505385630923806739843006211889453459797069615462956085590781191746057846544917859283603703265863428051719505388160156793149310240786072710949215191498854607302443139363423199668843610657009207412512620979232808547318135401828396784833552285253647562620080162410629362003123104573986503645890549517247021467988330265057881880613099352174359171507120540244626147892843129869190658207287179660451428570672040103235871904223818622943218649849939883710259156430273959794157954990212504923253114776428643752750103053886756279539870032860393794126578593164364866073238648672614874040993442305569000248035293795237889742374005867048917514881075247478648656378843625219480724314928890522548437191980527485737976828164509568486230142040244288223571414859128654340216328214644016475654632613182152808372173752006052194954911217189102689331004159646148774799848873345624414173919267804391864998526204590806322372842250927860843146156103324964069934313921386791453477143284366445616436309276697375688385972885000618494345821796197991057934760133706691721056715896225304374678424368331929134830692463208887235872391683565885787383515605337205821279015948625102763738244366576566017170369429820162988848962118186773058523329043082135416694822759423162417719609922169822154507412108560233558290712549532635619462731755108246397526294874709835307029972307816705129663336229796558376916962496259003532047980275001392170687210473129599869298803394050600841787245450501531330982092736225060677224444721166559992385098283961503684044345277459444234902281495876818299851405032984641737457546787401550835345155031203648621050079880392159151286752199892991877645340666263360022154976486238156861660744810578258980214424577095646864790332263285341278919910515499419886088666890120545874782793553334904194758340122595377998471372586016204749478268916830196653432514704380867570359047260242668152335255685355953702429594889208428795732619564630999746246623243290035920261395586101129457218197493435155634954232598304975914966702118993366304548391841990514431210620548421690017095817106766221048351888232889889940459134780109942723785345965468555310172328977915814789047367856723526286292868803220721151954561295077788458110199988827698079847262359969201611127936161327201766809925003701133229176812130830471228269740390786705361743608215802673712598473834339464677333061389444709997915205050830455921842617524338501075001146825674072210563233183762502072152131458615344313121868307863528190717959848355404205073623884842739921824241399546909941654094991649664705491220113897543246298579681263756063931878138881717398807381671028397443062664776437388842456327589100869496482496229778892043101057451558475987276478910515459186415426526246831929264331726887826526624445499970102345564007690177602917212972450729676016681420637191172198290129890488906577110447088691255926917674786283016406665988944947109988014525084395518216623028807453957345003276485520041915545085269092391996666510885331338860836982612065640555217865985686768394619547946042358005468174273611240981897896170040166580105652255942697946709238187703856170432536113982998956983225429245414381179025578606943950662612527051820591465316174925758918091528194284403894008359349083923123748347874347056798020139528383500814114465732227094031554439704133520045879231960721443102667577518932651262950441300804867919335275165242021114452514677133489344294161399096465998129892268381157404850686178492740124945031677799879727381302798785244023419130434414498724843221974814621693343743755317530255326797998252842356954235931388233504640840419385602957452323928297249816227392259072001 digits: 9155