sgarden-what is wrong in calculating cycle length this way??

This code gives wrong answer.
can anyone tell what is wrong??
http://www.codechef.com/viewsolution/4259512

You just took a mod at the end! This is wrong because an overflow would have already occurred in your for loop, thus storing a wrong value in your ans.

Now you will try to put
ans=ans%md;
after every interation of your loop. This is also wrong because it changes the value of ans for the next gcd calculation. See the editorial for more clarity. Basically you aren’t supposed to find out LCM this way. You have to use a technique where an overflow can be “modded” without changing the value of answer.

Actually you are taking mod after all operations has been done.But Actually if ans*len go bigger than 2^63-1 than it will return a garbage value.

Therefore you should use factors method to find the LCM of number.Carefully use mod.