I'm not an expert for c(++) programming, so I won't comment on your code too much.
Generally you should look for constraints and suitable data types in
problem statement
problem comments
forum (use the search engine)
Here I'm not sure if you used long long data type. The comments show some tricky test cases.
Good luck.
Daft