This is a C++03 port of arbitrary-precision arithmetic of integer library. It allows you input and output large integers from strings in any bases between 2 and 36.
The BigIntMini & BigIntTiny is designed for online contest that enought to use in most of cases although they are not the fastest.
Here is an example for calculate 2^100
#include <iostream>
#include "bigint_hex.h"
#include "bigint_dec.h"
int main() {
using namespace std;
BigIntHex s; // the same as BigIntDec
s = 1;
for (int i = 1; i <= 100; ++i) {
s *= BigIntHex(2);
}
cout << s.to_str() << endl;
return 0;
}
#include <iostream>
#include "bigint_mini.h"
int main() {
using namespace std;
BigIntMini s;
s = 1;
for (int i = 1; i <= 100; ++i) {
s = s * BigIntMini(2); // no operator*= overloading
}
cout << s.to_str() << endl;
return 0;
}
#include <iostream>
#include "bigint_tiny.h"
int main() {
using namespace std;
BigIntTiny s;
s = 1;
for (int i = 1; i <= 100; ++i) {
s = s * BigIntTiny(2); // no operator*= overloading
}
cout << s.to_str() << endl;
return 0;
}
The output is 1267650600228229401496703205376
Run build_singlefile.py
to generate the standalone header file, then copy the header file of the class which you need into the front of the source file.
For single_bigint_hexm.h
and single_bigint_decm.h
, they streamline the input and output of the base conversion, and since online competitions rarely need to do this step.
Copy all header files to your project's directory then include them.
BigIntHex a, b;
a = 123;
a = "123";
a = std::string("123");
a.set(123);
a.from_str("ABCDEF", 16);
a.from_str("1011010", 2);
a.from_str("123456789ABCDEFGZXY", 36);
b = a;
BigIntHex a, b;
a = a + b;
a += b;
BigIntHex a, b;
a = a - b;
a -= b;
BigIntHex a, b;
a = a * b;
a *= b;
a *= 123;
BigIntHex a, b;
a = a / b;
a /= b;
BigIntHex a, b;
a < b;
a <= b;
a >= b;
a > b;
a == b;
a != b;
BigIntHex a; // or BigIntDec
cout << a.to_str() << endl; // dec by default
cout << a.to_str(16) << endl; // hex output
BigIntMini b; // or BigIntTiny
cout << b.to_str() << endl;
operators | BigIntHex | BigIntDec | BigIntMini | BigIntTiny |
---|---|---|---|---|
constructor Bigint | ✔ | ✔ | ✔ | ✔ |
constructor int | ✔ | ✔ | ✔ | ✔ |
constructor char* | ✔ | ✔ | ❌ | ❌ |
constructor string | ✔ | ✔ | ✔ | ✔ |
=Bigint | ✔ | ✔ | ✔ | ✔ |
=int | ✔ | ✔ | ✔ | ✔ |
=string | ✔ | ✔ | ✔ | ✔ |
=char* | ✔ | ✔ | ✔ | ❌ |
<, ==, >, <=, >=, != Bigint | ✔ | ✔ | ✔ | ✔ |
+, -, *, /, % int | ❌ | ❌ | ❌ | ✔ |
+=, -=, *=, /=, %= int | ❌ | ❌ | ❌ | ❌ |
+, -, *, /, % Bigint | ✔ | ✔ | ✔ | ✔ |
+=, -=, *=, /=, %= Bigint | ✔ | ✔ | ❌ | ❌ |
Base conversion | ✔ | ✔ | ❌ | ❌ |
Efficiency | ✬✬✬✬ | ✬✬✬ | ✬✬ | ✬ |
This project is licensed under the MIT License.