Stack Overflow exception for two test cases

It is giving stack overflow exception for fact function, How I can resolve that.

provide the correct link.

I am really sorry.
This is the correct link.

sorry for the late reply @archit1jain i was quite busy… will you please provide me code using long instead of big integers because i code in c++ and i am not familiar with java big int and one thing I don’t think there is any need of using big integers except in rare cases, long is sufficient for 99 % questions. i never find any need of big int in c++. It only increases the time complexity of your program.

But How I can store N! in long.? where N<=10^5.

store it modulo (1e9-1) . I think you don’t know that ( a^b ) % c is same as ( (a) ^ ( b% (c-1) ) ) % c.

it is when c is prime na.


Here is the code as long datatype.
I am getting wrong ans for the last test case.

sory i dint see that p is variable. what you can do is that… find a^1 … a^2… a^3… a^4… a^5… a^b then multiply each of them. since a^b will take log(b) time and total n terms are there in factorial so nlog(b) total time. it should work fine.

there are test cases too. So it is giving time limit.

i submitted with same approach, all test cases were passed . In which tc you are getting tle?
also share the code link.


getting wrong ans for every test case except one.

sory my bad i told you to multiply each term … thats wrong it should be (((a^2)^3)^4…)^n). I have modified your code you can check it.

1 Like
1 Like

Thank you so much.
I also posted doubt for SUMSSUM question, but it is till not resolved.

did you have asked this Q sumssum before also ?

1 Like

Yes. I did, but now I posted it again

apaar have acknowleged your doubt pm him.