📌  相关文章
📜  求两个整数 X 和 Y,给定 GCD P 和它们的平方差 Q

📅  最后修改于: 2022-05-13 01:56:04.747000             🧑  作者: Mango

求两个整数 X 和 Y,给定 GCD P 和它们的平方差 Q

给定两个整数PQ ,任务是找到最大公约数(GCD)为P且平方差为 Q 的任意两个整数。如果不存在任何这样的整数,则打印“-1”

例子:

方法:给定的问题可以使用基于以下观察来解决:

给定的方程也可以写成:

现在对于给定方程的积分解:

让 (x + y) = p1 和 (x + y) = p2
是两个方程,其中 p1 和 p2 是Q的除数
这样p1 * p2 = Q

求解上述两个方程,我们有:

从以上计算可知,要使x 和 y为整数,则除数之和必须为偶数。由于 x 和 y 的两个值有 4 个可能的值,即(+x, +y)、(+x, -y)、 (-x, +y) 和 (-x, -y)
因此,可能解决方案的总数由4*(count of divisors with even sum)给出。

现在在这些对中,找到 GCD 为 P 的对并打印该对。如果不存在这样的对,则打印 -1。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print a valid pair with
// the given criteria
int printValidPair(int P, int Q)
{
 
    // Iterate over the divisors of Q
    for (int i = 1; i * i <= Q; i++) {
 
        // check if Q is a multiple of i
        if (Q % i == 0) {
 
            // L = (A - B) <- 1st equation
            // R = (A + B) <- 2nd equation
            int L = i;
            int R = Q / i;
 
            // Calculate value of A
            int A = (L + R) / 2;
 
            // Calculate value of B
            int B = (R - L) / 2;
 
            // As A and B both are integers
            // so the parity of L and R
            // should be the same
            if (L % 2 != R % 2) {
                continue;
            }
 
            // Check the first condition
            if (__gcd(A, B) == P) {
                cout << A << " " << B;
                return 0;
            }
        }
    }
 
    // If no such A, B exist
    cout << -1;
 
    return 0;
}
 
// Driver Code
int main()
{
    int P = 3, Q = 27;
    printValidPair(P, Q);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to print a valid pair with
// the given criteria
static int printValidPair(int P, int Q)
{
 
    // Iterate over the divisors of Q
    for (int i = 1; i * i <= Q; i++) {
 
        // check if Q is a multiple of i
        if (Q % i == 0) {
 
            // L = (A - B) <- 1st equation
            // R = (A + B) <- 2nd equation
            int L = i;
            int R = Q / i;
 
            // Calculate value of A
            int A = (L + R) / 2;
 
            // Calculate value of B
            int B = (R - L) / 2;
 
            // As A and B both are integers
            // so the parity of L and R
            // should be the same
            if (L % 2 != R % 2) {
                continue;
            }
 
            // Check the first condition
            if (__gcd(A, B) == P) {
                System.out.print(A+ " " +  B);
                return 0;
            }
        }
    }
 
    // If no such A, B exist
    System.out.print(-1);
    return 0;
 
}
static int __gcd(int a, int b) 
{ 
    return b == 0? a:__gcd(b, a % b);    
}
   
// Driver Code
public static void main(String[] args)
{
    int P = 3, Q = 27;
    printValidPair(P, Q);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# python program for the above approach
import math
 
# Function to print a valid pair with
# the given criteria
def printValidPair(P, Q):
 
    # Iterate over the divisors of Q
    for i in range(1, int(math.sqrt(Q)) + 1):
 
        # check if Q is a multiple of i
        if (Q % i == 0):
 
            # L = (A - B) <- 1st equation
            # R = (A + B) <- 2nd equation
            L = i
            R = Q // i
 
            # Calculate value of A
            A = (L + R) // 2
 
            # Calculate value of B
            B = (R - L) // 2
 
            # As A and B both are integers
            # so the parity of L and R
            # should be the same
            if (L % 2 != R % 2):
                continue
 
            # Check the first condition
            if (math.gcd(A, B) == P):
                print(f"{A} {B}")
                return 0
 
    # If no such A, B exist
    print(-1)
 
    return 0
 
# Driver Code
if __name__ == "__main__":
 
    P = 3
    Q = 27
    printValidPair(P, Q)
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to print a valid pair with
    // the given criteria
    static int printValidPair(int P, int Q)
    {
 
        // Iterate over the divisors of Q
        for (int i = 1; i * i <= Q; i++)
        {
 
            // check if Q is a multiple of i
            if (Q % i == 0)
            {
 
                // L = (A - B) <- 1st equation
                // R = (A + B) <- 2nd equation
                int L = i;
                int R = Q / i;
 
                // Calculate value of A
                int A = (L + R) / 2;
 
                // Calculate value of B
                int B = (R - L) / 2;
 
                // As A and B both are integers
                // so the parity of L and R
                // should be the same
                if (L % 2 != R % 2)
                {
                    continue;
                }
 
                // Check the first condition
                if (__gcd(A, B) == P)
                {
                    Console.Write(A + " " + B);
                    return 0;
                }
            }
        }
 
        // If no such A, B exist
        Console.Write(-1);
        return 0;
 
    }
    static int __gcd(int a, int b)
    {
        return b == 0 ? a : __gcd(b, a % b);
    }
 
    // Driver Code
    public static void Main()
    {
        int P = 3, Q = 27;
        printValidPair(P, Q);
    }
}
 
// This code is contributed by gfgking


Javascript



输出:
6 3

时间复杂度: O(sqrt(Q))
辅助空间: O(1)