//CODE DELETED
I wrote the above code, i solved it mathematically and then came out to the conclusion that we do not need gcd for this question if we frame it like this. Anyone can help...?
Compile this in c++ 4.8.1 in ideone.com once and run the test cases, it gives correct answers.