Calculate a^b - b^a

Calculate the value of a^b - b^a .

Range : 1 <= a , b <= 100.

This can easily be done in python but I need a solution using C++?

What should I do to avoid overflow. ( Cannot use MOD also ).

Problem Link.

Implementing BigInteger library yourself seems to be the only option :confused:

Yes :-/
Any other suggestion ? Anyone ?

Nothing else is possible except taking then in a char array and doing multiplication and subtraction by your own method… Because let’s say inputs are
2 100
Then “answer”(i.e. 2^100 - 100^2 )may even exceed limit of long long int… Hence it is not possible to store it in anything except char array… Hence I think this is the only solution…

As said by @vijju123

@guitarlover you can use Boost C++'s Boost Multiprecision( but then again, to the best of my knowledge Boost is not supported on OJ’s so you have no option other than what @l_returns mentioned. If you want to avoid boilerplate code, look at :

Don’t do it in a char array, that’s just a bad idea. I would recommend picking some large base (you could easily do base 2^{32} if you wanted to) and then compute using that on an array of integers (exact size depending on your base). You can easily work in huge numbers with comparatively few digits that way.


As mentioned I created 2 arrays with which stores the sum of a^b and b^a.
Then I subtracted one array from the other.
Sounds little weird.

** Here is my AC Solution ( C++ ) **

Got AC with array of integers.