Hello codeforces, I am up-solving ECPC qualifications day 2 and i need some help in problem D.
After trying some tests i found that there is a pattern that repeat. and all the numbers between 0 and B-1 that is divisible by the GCD(A,B). And by the way I can easily calculate the the length of this cycle and also the sum of its number. the length = B/GCD(A,B)
and the sum = (length*(length - 1)/2) * A
Now we can solve the repeated cycles easily. But my problem is how to calculate the tail of it in O(1) .. i.e. lets say A = 1, B = 7, C = 10 ... we have only one repeated cycle which is from 0 to 6 and after that we will have remaining 3 number from the cycle. so we will need to calculate this what i call tail in O(1). as O(N) will get TLE due to the high test cases numbers.
Can anyone please help me with this problem?