Way to use big integer in C++

Hello all,

I would like to share a library which provides integer, rational and floating-point types in C++ that have more range and precision than C++'s ordinary built-in types. You can easily use this library to get values equal to even 100 ! (100 factorial) and larger. This library also extends expression template support to standard library functions like abs or sin with number arguments

I will give code example below so that you can use it instead of array multiplication or division and get fast AC in easy problems which require arithmetic operations on large numbers.

If you want to read more about it, you can see here.

Code to compute 100! and 99! : link

Here are some problems where you can try this library :

FCTRL2

MULTISRM

This library works fine on CodeChef and HackerEarth. However, it doesn’t works on HackerRank and CodeForces.

Thank You !!

8 Likes

Hello , you guys can even refer to this Bigint Class which will work everywhere you just need to include it in your solution and it is really simple to use . It provides nearly every operations and they are optimised to quite an extent (it uses karatsuba for multiplication and optimised memory). Btw when using this in a contest just add a comment in your code explaining the link the class to make sure that no issues raises .

cheers !

3 Likes

@torque I have solved the question MULTISRM by using this library.it is running correctly on ideone link but it gives compilation error on codechef and also on codeblocks.some of the errors are:-
prog.cpp:1:44: error: boost/multiprecision/cpp_int.hpp: No such file or directory
prog.cpp:3: error: ‘boost’ has not been declared
prog.cpp:3: error: ‘multiprecision’ is not a namespace-name
prog.cpp:3: error: expected namespace-name before ‘;’ token

You must be using C++ 4.3.2. Use C++ 14. It should not give compilation error in C++14

@torque , Can I use this to directly solve the Lucky Number 72 (LUCK72) problem that appeared on NIT Kurukshetra code battle 2 yesterday? If not, then what is the possible range of the integers that this library can be used to operate upon?

Yes, you can use it in such questions but it would give TLE on LUCKY72 as operations are costly in this header file and there are MOD operations involved in LUCKY72 which are even more costly.

Hi
Providing a link to post on codeforces explaining how to use big integers in c++.

http://codeforces.com/blog/entry/16380

Happy Coding…

@torque this program is working on codechef ide while showing error in hackerrank.
solution.cc:1:9: fatal error: boost/multiprecision/cpp_int.hpp: No such file or directory.
`#include<boost/multiprecision/cpp_int.hpp>
#include
namespace mp = boost::multiprecision;
using namespace std;

int main()
{
int n;
cin>>n;
mp::cpp_int fac=1;
for(int i=1;i<=n;i++)
{
fac=fac*i;
}
cout<<fac;
return 0;
}
`can you please tell me?

he,@torque mentioned that this doesn’t work in hackerrank and codeforces !