Problem LinkAuthor: Bhuvnesh Jain Tester: Mark Mikhno Editorialist: Bhuvnesh Jain DifficultyEASYMEDIUM PrerequisitesGCD, Modular Exponentiation, Overflowhandling ProblemFind the GCD of $A^N + B^N$ and $(A  B)$ modulo $1000000007$. ExplanationThe only property required to solve the complete problem is $GCD(U, V) = GCD(U \% V, V)$. If you are unfamiliar with this, you can see the proof here. Now the problem remains finding the value of $(A^N + B^N) % (A  B)$. This is can be easily done using modular exponentiation in $O(\log{N})$ complexity. You can read about on wikipedia and implementation at Geeks for Geeks. With the above 2 things, you are almost close to the full solution. The only thing left now is to handle overflows in languages like C++ and Java. First, understand why we might get overflow and then how we handle it. Note that we are computing $A^N % (A  B)$. Since, $(A  B)$ can be of the order ${10}^{12}$, the intermediate multiplications during exponentiation can be of the order of ${10}^{12} * {10}^{12} = {10}^{24}$ which is too large to fit in long long data type. Due, to overflows, you will get the wrong answer. To deal with overflows, below are 3 different methods:
This approach has a complexity of $O(1)$.
This approach has a complexity of $O(\log{B})$.
The time complexity of this approach is $O(1)$. The final corner case to solve the problem is the case when $A = B$. This is because calculating $A^N + B^N % (A  B)$, would lead to runtime error while calculating modulo $0$. For this case, we use the fact that $GCD(U, 0) = U$. Thus the answer is simply $A^N + B^N$. The last part is just printing the answer modulo $1000000007$. The overall time complexity is $O(\log{N} + \log{max(A, B)})$. The first is for calculating the modular exponentiation and the second part is for calculating GCD. The space complexity is $O(1)$. Once, you are clear with the above idea, you can see the author implementation below for help. Note that since the number of test cases was small, another approach which iterates over divisors of $(A  B)$ to find the answer will also pass within the time limit if proper care is taken of overflows and the case $A = B$. Feel free to share your approach as well, if it was somewhat different. Time Complexity$O(\log{N} + \log{max(A, B)})$ Space Complexity$O(1)$ SOLUTIONS:Author's solution can be found here. Tester's solution can be found here. mgch's solution can be found here.
This question is marked "community wiki".
asked 13 Aug '18, 15:07

Have a look at my "N independent " solution(except when a=b), of course it is wrong but it still passes . Test cases where it should have failed.. some test cases :
there are many many more test cases where it should have failed. Test cases were very weak. I understand that making good test cases is difficult task but this time they are extremely weak. answered 14 Aug '18, 07:21

"With the above 2 things, you are almost close to the full solution. The only thing left now is to handle overflows in languages like C++ " I was confused on this since day 1. https://www.codechef.com/viewsolution/19465269 long long passed for me in C++, and I dont know why. And my friends got multiple WAs because of overflow, what is there in my approach that I avoided it in the step 
answered 13 Aug '18, 15:16
I can't find anything like that in your code..
(13 Aug '18, 16:06)
THESE COMMENT I TRIED TO DELETE A LOT OF TIMES BUT IT WON'T ^^^^^
(13 Aug '18, 16:34)
i is integer and u assigned it a long long value !!
(13 Aug '18, 16:35)
Assume high level mathematics unless proven otherwise? :p XD Though I feel it should get WA. I was surprised it didnt even added an assert to check for overflow!
(14 Aug '18, 01:30)

Anyone who not got the editorial: A>B let y=AB so A=y+B now :A^n+B^n =(y+B)^n+B^n expand this y * (some value)+2 * B^n since first part is already mutiple of y Answer will be gcd of(y,2*B^n) it can be done easily!! answered 13 Aug '18, 15:26

Answer doesn't depend on N except some special case like a=b or N=1 or N=2.If N>2 we can assume N=2 and answer will be same.I don't know why it gives correct answer. Due to weak test cases it passes even if don't use different approaches for N=1 and N>1. like for A=10,B=2, N=1 my solution gives answer 8 but correct answer is 4.similarly for A=12 , B=3 , N=1. my solution answered 13 Aug '18, 15:44

It's simple gcd(a, b) = gcd(b, a % b) Similarly gcd(a^{n} + b^{n}, a  b) is gcd(a  b, 2 * b^{n}) since (a^{n} + b^{n}) % (a  b) is 2 * b^{n}. It can be seen very easily with polynomial division. It's very simple to find now !! This is my solution with prime factorization Solution answered 13 Aug '18, 15:57

@venturer  you say
but this is not correct. The follow test case would distinguish: Input
Output
It's frustrating that there are two editorial threads for GCDMOD, can the mods amalgamate somehow? Or even just lock one of them? answered 14 Aug '18, 04:45
There are discussions going in at least 2 of them  I cant delete them hence. I am seeing what can be done, if needed.
(14 Aug '18, 15:12)

Hello GCD(A+B,AB)=GCD(AA+BB,AB)=GCD(AAA+BBB,AB) and so on...; therefore solution is GCD(A+B,AB); answered 14 Aug '18, 09:46

Can anyone explain me why my solution is giving tle and test cases where it fails answered 14 Aug '18, 11:26

As mentioned by @khiljee and @bvsbrk in the above posts, I used the fact that gcd(a^n + b^n, ab) = gcd(2*b^n, ab) Use prime factorization to find GCD as below: I was failing some test cases while using the prime factorization approach as above. Which I fixed while the competition approached its end. The case being, we can compute primes for My solution here answered 14 Aug '18, 15:41

Can somebody tell me whats wrong in the following approach? Let G be gcd(A^N+B^N, AB) (assuming A > B)
answered 16 Aug '18, 00:45

Hi @shahanurag, your assumption can be invalidated using the following:
Try it out for A = 14, B = 24 and M = 5:
I too got stuck at this point! But the following holds true:
So, we can say that: GCD(A^{N}+B^{N}, AB) = GCD(AB, (A^{N}+B^{N})%(AB)) Thus while calculating (A^{N}+B^{N})%M take M=AB and the answer would be GCD(M, (A^{N}+B^{N})%M) % 1e9+7 Here's my solution answered 16 Aug '18, 01:12
Thank you, for a few days, I was stuck at this idea trying to prove or invalidate it. Finally I scrapped it, and now it turns out it was wrong. Thanks for giving a fail case :)
(16 Aug '18, 10:23)
No problem mate. I'm glad that you found this helpful :)
(16 Aug '18, 13:47)

My solution was to notice that $A  B$ is smaller than $A ^ N + B ^ N$. Then we can find all the factors of $A  B$, and for each one of them check whether or not that factor of $A  B$ divides $A ^ N + B ^ N$ and by taking the maximum of all of these numbers we find the $gcd(A  B, A ^ N + B ^ N)$. Time complexity  $O(TMlog N)$, $M = 10^6$ answered 16 Aug '18, 09:24

Can't we use: long long d = (bpow(a, n, MOD) + bpow(b, n, MOD)) % (MOD);//MOD=10^9+7 instead of long long d = (bpow(a, n, a  b) + bpow(b, n, a  b)) % (a  b); answered 17 Aug '18, 01:32

Can anyone explain me the prod function in tester's solution,
I see it is similar to fast exponentiation but I don't really get this code answered 25 Aug '18, 14:12

Can anyone please explain me why first code https://www.codechef.com/viewsolution/21531950 gets TLE while the second one https://www.codechef.com/viewsolution/21531975 doesn't. The only difference between them is in loop condition. In first its (i*i)<=d while in second its i<=(d/i). answered 08 Nov '18, 09:50

Please fix this incomplete sentence  "The only property required to solve the complete problem is GCD(U,V)=GCD(U."
edited that @utkalsinha but I am not sure whether it will really be edited or not as discuss is facing some issues nowadays...
Thanks @l_returns
The links to the solutions are a bit mixed up. Currently, the solution marked as author solution is actually using int128.