# Problem Link

**Author:** Bhuvnesh Jain

**Tester:** Mark Mikhno

**Editorialist:** Bhuvnesh Jain

# Difficulty

EASY-MEDIUM

# Prerequisites

GCD, Modular Exponentiation, Overflow-handling

# Problem

Find the GCD of A^N + B^N and (A - B) modulo 1000000007.

# Explanation

The 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:

- Using â€śint_128â€ť inbuilt datatype in C++. For details, you can refer to [mgch solution].

This approach has a complexity of O(1).

- Using an idea similar to modular exponentiation. Instead of carrying out multiplication operations, we use addition operations. Below is a pseudo-code for it:

```
# Returns (a * b) % m
def mul_mod(a, b, m):
x = 0, y = a
while b > 0:
if b & 1:
# No overflows in additons.
x = (x + y) % m
y = (y + y) % m
b >>= 1
return x
```

This approach has a complexity of O(\log{B}).

- Using idea similar to karatsuba trick. This is specific only to this question as constraints are upto {10}^{12} and not {10}^{18}. We can split a as a_1 * {10}^{6} + a_2 and b as b_1 * {10}^{6} + b_2. Note that all a_1, b_1, a_2, b_2 are now less than or equal to {10}^{6}. Now we multiply both of them and there will be no overflow now in intermediate multiplications as the maxmium value can be {10}^{12} * max(a_1, b_1) = {10}^{18}. The setter code using this approach.

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.