HDU 1134 modulo of large numbers use a large number Template

Source: Internet
Author: User
Tags integer division
# Include <iostream> # include <string> # include <iomanip> # include <algorithm> using namespace STD; char STR [1010]; int modd; # define maxn 9999 # define maxsize 10 # define dlen 4 class bignum {PRIVATE: int A [1010]; // The number of digits that can be controlled for a large number int Len; // the length of a large number is public: bignum () {Len = 1; memset (A, 0, sizeof (a);} // constructor bignum (const INT ); // convert a variable of the int type to the bignum (const char *); // convert a variable of the string type to the bignum (const bignum &); // copy the constructor bignu M & operator = (const bignum &); // reload the value assignment operator to assign values between large numbers: Friend istream & operator> (istream &, bignum &); // reload the input operator friend ostream & operator <(ostream &, bignum &); // reload the output operator bignum operator + (const bignum &) const; // reload the addition operator, bignum operator-(const bignum &) const; // overload subtraction operator, subtraction between two large numbers bignum operator * (const bignum &) const; // reload the multiplication operator. The multiplication operation between two large numbers is bignum operator/(const Int &) const; // reload the Division operator. Integer Division bignum operator ^ (const Int &) const; // Npower operation of large numbers int operator % (const Int &) const; // perform the modulo operation bool operator> (const bignum & T) const on an int type variable in a large number; // compare the size of a large number with that of another large number bool operator> (const Int & T) const; // compare the size of a large number with that of an int Type Variable Void print (); // output large number}; bignum: bignum (const int B) // convert an int type variable to a large number {int C, D = B; Len = 0; memset (A, 0, sizeof (a); While (D> maxn) {c = D-(D/(maxn + 1) * (maxn + 1 ); D = D/(MA Xn + 1); A [Len ++] = C;} A [Len ++] = D;} bignum: bignum (const char * s) // convert a variable of the string type to a large number {int T, K, index, l, I; memset (A, 0, sizeof ()); L = strlen (s); Len = L/dlen; If (L % dlen) Len ++; Index = 0; for (I = L-1 I> = 0; i-= dlen) {T = 0; k = I-dlen + 1; if (k <0) k = 0; For (Int J = K; j <= I; j ++) t = T * 10 + s [J]-'0'; A [index ++] = T ;}} bignum: bignum (const bignum & T): Len (T. len) // copy the constructor {int I; memset (A, 0, sizeof (a); for (I = 0; I <Len; I ++) A [I] = T. A [I];} Bignum & bignum: Operator = (const bignum & N) // reload the value assignment operator and assign values between large numbers {int I; Len = n. len; memset (A, 0, sizeof (a); for (I = 0; I <Len; I ++) A [I] = n. A [I]; return * This;} istream & operator> (istream & in, bignum & B) // reload the input operator {char ch [maxsize * 4]; int I =-1; in> CH; int L = strlen (CH); int COUNT = 0, sum = 0; for (I = L-1; I> = 0 ;) {sum = 0; int T = 1; for (Int J = 0; j <4 & I> = 0; j ++, I --, T * = 10) {sum + = (CH [I]-'0') * t;} B. A [count] = sum; Count ++;} B. len = count ++; Return in;} ostream & operator <(ostream & out, bignum & B) // reload the output operator {int I; cout <B. A [B. len-1]; for (I = B. len-2; I> = 0; I --) {cout. width (dlen); cout. fill ('0'); cout <B. A [I];} return out;} bignum: Operator + (const bignum & T) const // The addition operation between two large numbers {bignum T (* This ); int I, big; // The number of digits big = T. len> Len? T. len: Len; for (I = 0; I <big; I ++) {T. A [I] + = T. A [I]; If (T. A [I]> maxn) {T. A [I + 1] ++; T. A [I]-= maxn + 1 ;}} if (T. A [Big]! = 0) T. len = big + 1; elset. len = big; return t;} bignum: Operator-(const bignum & T) const // subtraction between two large numbers {int I, j, big; bool flag; bignum T1, T2; If (* This> T) {T1 = * This; t2 = T; flag = 0;} else {T1 = T; t2 = * this; flag = 1 ;}big = t1.len; for (I = 0; I <big; I ++) {If (t1.a [I] <t2.a [I]) {J = I + 1; while (t1.a [J] = 0) J ++; t1.a [j --] --; while (j> I) t1.a [j --] + = maxn; t1.a [I] + = maxn + 1-t2.a [I];} elset1.a [I]-= t2.a [I];} t1.len = big; while (t1.a [Len-1] = 0 & t1.len> 1) {t1.len --; big --;} If (FLAG) t1.a [Big-1] = 0-t1.a [Big-1]; return T1;} bignum: Operator * (const bignum & T) const // multiplication between two large numbers {bignum ret; int I, j, up; int temp, temp1; for (I = 0; I <Len; I ++) {up = 0; For (j = 0; j <t. len; j ++) {temp = A [I] * t. A [J] + ret. A [I + J] + up; If (temp> maxn) {temp1 = temp-temp/(maxn + 1) * (maxn + 1); up = Temp/(maxn + 1); ret. A [I + J] = temp1;} else {up = 0; ret. A [I + J] = temp ;}} if (up! = 0) ret. A [I + J] = up;} ret. len = I + J; while (Ret. A [ret. len-1] = 0 & ret. len> 1) ret. len --; return ret;} bignum: Operator/(const Int & B) const // perform the division operation on an integer {bignum ret; int I, down = 0; for (I = len-1; I> = 0; I --) {ret. A [I] = (a [I] + down * (maxn + 1)/B; down = A [I] + down * (maxn + 1)-ret. A [I] * B;} ret. len = Len; while (Ret. A [ret. len-1] = 0 & ret. len> 1) ret. len --; Return ret;} int bignum: Operator % (const Int & B) const // a large number of Modulo operations on an int type variable {int I, D = 0; for (I = len-1; I> = 0; I --) {d = (D * (maxn + 1) % B + A [I]) % B ;} return D;} bignum: operator ^ (const Int & N) const // Npower operation of large numbers {bignum T, RET (1); int I; if (n <0) Exit (-1); If (n = 0) return 1; if (n = 1) return * This; int M = N; while (M> 1) {T = * this; for (I = 1; I <1 <= m; I <= 1) {T = T * t ;} m-= I; ret = RET * t; If (M = 1) ret = RET * (* This);} return RET;} bool bignum: Operator> (const bignum & T) const // compare the size of a large number with that of another one {int ln; If (LEN> T. len) return true; else if (LEN = T. len) {Ln = len-1; while (A [Ln] = T. A [Ln] & ln> = 0) ln --; If (LN> = 0 & A [Ln]> T. A [Ln]) return true; elsereturn false;} bool bignum: Operator> (const Int & T) const // compare the size of a large number and an int type variable {bignum B (t); return * This> B;} void bignum: Print () // output large number {int I; cout <A [Len-1]; for (I = len-2; I> = 0; I --) {cout. width (dlen); cout. fill ('0'); cout <A [I];} cout <Endl;} int main (void) {While (~ Scanf ("% S % d", STR, & modd) {bignum big (STR); cout <big % modd <Endl ;}return 0 ;}

Using a large number template, it is easy to be speechless... For more information about the template, see http://blog.csdn.net/vsooda/article/details/8543351.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.