Find GCD between the sum of two given integers raised to the power of N and their difference
Last Updated :
12 Apr, 2021
Given three positive integers, P, Q and N, the task is to find the GCD of (PN + QN) and (P – Q) under modulo 109 + 7.
Examples:
Input: p = 10, q = 6, n = 5
Output: 4
Explanation: pn + qn = 105 + 65 = 107776 and p – q = 4. GCD of 107776 and 4 is 4.
Input: p = 7, q = 2 and n = 5
Output: 1
Explanation: pn + qn = 75 + 25 = 16839 and p – q = 5. GCD of 16839 and 5 is 1.
Approach: Since the number (pn + qn) can be extremely large, such a huge number cannot be stored in any data type and thus GCD cannot be calculated using the Euclidean algorithm. Therefore, modular arithmetic can be used to find the answer.
Follow the below steps to solve this problem:
- To find the GCD of a big number and a small number, find the divisors of the smaller number in O(?p-q).
- These numbers are the potential GCD candidates.
- Now, check if any of these potential GCDs divides the larger number. The largest number which divides both the numbers is the final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
long long int power( long long a, long long n,
long long int d)
{
long long int res = 1;
while (n) {
if (n % 2) {
res = ((res % d) * (a % d)) % d;
}
a = ((a % d) * (a % d)) % d;
n /= 2;
}
return res;
}
long long int gcd( long long p, long long q,
long long n)
{
if (p == q) {
return (power(p, n, mod)
+ power(q, n, mod))
% mod;
}
long long int candidate = 1;
long long int num = p - q;
long long int sq = sqrt (num);
for ( long long i = 1; i <= sq; ++i) {
if (num % i == 0) {
long long int X = power(p, n, i);
long long int Y = power(q, n, i);
long long int temp = (X + Y) % i;
if (temp == 0) {
candidate = max(candidate, i);
}
temp = (power(p, n, num / i)
+ power(q, n, num / i))
% (num / i);
if (temp == 0) {
candidate = max(candidate, num / i);
}
}
}
return candidate % mod;
}
int main()
{
long long int p, q, n;
p = 10;
q = 6;
n = 5;
cout << gcd(p, q, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int mod = 1000000007 ;
static int power( int a, int n, int d)
{
int res = 1 ;
while (n != 0 ) {
if ((n % 2 ) != 0 ) {
res = ((res % d) * (a % d)) % d;
}
a = ((a % d) * (a % d)) % d;
n /= 2 ;
}
return res;
}
static int gcd( int p, int q, int n)
{
if (p == q) {
return (power(p, n, mod)
+ power(q, n, mod))
% mod;
}
int candidate = 1 ;
int num = p - q;
int sq = ( int )Math.sqrt(num);
for ( int i = 1 ; i <= sq; ++i) {
if (num % i == 0 ) {
int X = power(p, n, i);
int Y = power(q, n, i);
int temp = (X + Y) % i;
if (temp == 0 ) {
candidate = Math.max(candidate, i);
}
temp = (power(p, n, num / i)
+ power(q, n, num / i))
% (num / i);
if (temp == 0 ) {
candidate = Math.max(candidate, num / i);
}
}
}
return candidate % mod;
}
public static void main(String[] args)
{
int p, q, n;
p = 10 ;
q = 6 ;
n = 5 ;
System.out.println(gcd(p, q, n));
}
}
|
Python3
import math
mod = 1000000007 ;
def power(a, n, d):
res = 1 ;
while (n ! = 0 ):
if ((n % 2 ) ! = 0 ):
res = ((res % d) * (a % d)) % d;
a = ((a % d) * (a % d)) % d;
n / = 2 ;
return res;
def gcd(p, q, n):
if (p = = q):
return (power(p, n, mod) + power(q, n, mod)) % mod;
candidate = 1 ;
num = p - q;
sq = ( int )(math.sqrt(num));
for i in range ( 1 , sq):
if (num % i = = 0 ):
X = power(p, n, i);
Y = power(q, n, i);
temp = (X + Y) % i;
if (temp = = 0 ):
candidate = max (candidate, i);
temp = (power(p, n, num / i) + power(q, n, num / i)) % (num / i);
if (temp = = 0 ):
candidate = max (candidate, num / i);
return candidate % mod;
if __name__ = = '__main__' :
p = 10 ;
q = 6 ;
n = 5 ;
print (( int )(gcd(p, q, n)));
|
C#
using System;
class GFG
{
static int mod = 1000000007;
static int power( int a, int n, int d)
{
int res = 1;
while (n != 0)
{
if ((n % 2) != 0)
{
res = ((res % d) * (a % d)) % d;
}
a = ((a % d) * (a % d)) % d;
n /= 2;
}
return res;
}
static int gcd( int p, int q, int n)
{
if (p == q)
{
return (power(p, n, mod) + power(q, n, mod))
% mod;
}
int candidate = 1;
int num = p - q;
int sq = ( int )Math.Sqrt(num);
for ( int i = 1; i <= sq; ++i) {
if (num % i == 0) {
int X = power(p, n, i);
int Y = power(q, n, i);
int temp = (X + Y) % i;
if (temp == 0) {
candidate = Math.Max(candidate, i);
}
temp = (power(p, n, num / i)
+ power(q, n, num / i))
% (num / i);
if (temp == 0) {
candidate
= Math.Max(candidate, num / i);
}
}
}
return candidate % mod;
}
static public void Main()
{
int p, q, n;
p = 10;
q = 6;
n = 5;
Console.WriteLine(gcd(p, q, n));
}
}
|
Javascript
<script>
const mod = 1000000007;
function power(a , n , d) {
var res = 1;
while (n != 0) {
if ((n % 2) != 0) {
res = ((res % d) * (a % d)) % d;
}
a = ((a % d) * (a % d)) % d;
n /= 2;
}
return res;
}
function gcd(p , q , n)
{
if (p == q)
{
return (power(p, n, mod) + power(q, n, mod)) % mod;
}
var candidate = 1;
var num = p - q;
var sq = parseInt( Math.sqrt(num));
for (i = 1; i <= sq; ++i) {
if (num % i == 0) {
var X = power(p, n, i);
var Y = power(q, n, i);
var temp = (X + Y) % i;
if (temp == 0) {
candidate = Math.max(candidate, i);
}
temp = (power(p, n, num / i) + power(q, n, num / i)) % (num / i);
if (temp == 0) {
candidate = Math.max(candidate, num / i);
}
}
}
return candidate % mod;
}
var p, q, n;
p = 10;
q = 6;
n = 5;
document.write(gcd(p, q, n));
</script>
|
Time Complexity: O(sqrt(p – q))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...