Same code in Java gives RE that gives AC in C++.

I was doing this question. I implemented the same codes in Java and C++. This java code gives RE while the same code when implemented in C++(here) gives AC. Why is this happening?

i dont think they are quite same then

In C++, you are using unsigned long long, but in Java you are using long. Range of unsigned long long is two times the range of long. Since, long is not sufficient for this problem and there is no concept as unsigned in Java, you should use BigInteger in Java to get AC.