You use “long int” which is just a 32 bit signed integer on most systems.
Going through the accepted solutions it seems the solution doesn’t fit in a “long int” always. Use either a “unsigned long int” or “long long int” most accepted solutions in C/C++ use one of these.