📜  如果存在有限解,则找到线性Diophantine方程的初始积分解

📅  最后修改于: 2021-04-29 16:43:34             🧑  作者: Mango

给定三个整数abc ,其形式为: ax + by = c 。如果存在有限解,则任务是找到给定方程的初始积分解。

例子:

方法:

  1. 首先检查a和非零。
  2. 如果它们都为零且c为非零,则不存在解决方案。如果c也为零,则存在无限解。
  3. 对于给定的ab ,使用扩展欧几里德算法计算x 1 ,y 1gcd的值。
  4. 现在,要使存在的解决方案gcd(a,b)应该是c的倍数。
  5. 计算方程的解如下:
    x = x1 * ( c / gcd )
    y = y1 * ( c / gcd )
    

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include  
using namespace std;
  
// Function to implement the extended
// euclid algorithm
int gcd_extend(int a, int b,
               int& x, int& y)
{
    // Base Case
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
  
    // Recursively find the gcd
    else {
        int g = gcd_extend(b,
                           a % b, x, y);
        int x1 = x, y1 = y;
        x = y1;
        y = x1 - (a / b) * y1;
        return g;
    }
}
  
// Function to print the solutions of
// the given equations ax + by = c
void print_solution(int a, int b, int c)
{
    int x, y;
    if (a == 0 && b == 0) {
  
        // Condition for infinite solutions
        if (c == 0) {
            cout
                << "Infinite Solutions Exist"
                << endl;
        }
  
        // Condition for no solutions exist
        else {
            cout
                << "No Solution exists"
                << endl;
        }
    }
    int gcd = gcd_extend(a, b, x, y);
  
    // Condition for no solutions exist
    if (c % gcd != 0) {
        cout
            << "No Solution exists"
            << endl;
    }
    else {
  
        // Print the solution
        cout << "x = " << x * (c / gcd)
             << ", y = " << y * (c / gcd)
             << endl;
    }
}
  
// Driver Code
int main(void)
{
    int a, b, c;
  
    // Given coefficients
    a = 4;
    b = 18;
    c = 10;
  
    // Function Call
    print_solution(a, b, c);
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
static int x, y; 
  
// Function to implement the extended
// euclid algorithm
static int gcd_extend(int a, int b)
{
      
    // Base Case
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
  
    // Recursively find the gcd
    else
    {
        int g = gcd_extend(b, a % b);
        int x1 = x, y1 = y;
        x = y1;
        y = x1 - (a / b) * y1;
        return g;
    }
}
  
// Function to print the solutions of
// the given equations ax + by = c
static void print_solution(int a, int b, int c)
{
    if (a == 0 && b == 0) 
    {
          
        // Condition for infinite solutions
        if (c == 0)
        {
            System.out.print("Infinite Solutions " +
                             "Exist" + "\n");
        }
  
        // Condition for no solutions exist
        else 
        {
            System.out.print("No Solution exists" + 
                             "\n");
        }
    }
    int gcd = gcd_extend(a, b);
  
    // Condition for no solutions exist
    if (c % gcd != 0)
    {
        System.out.print("No Solution exists" + "\n");
    }
    else
    {
          
        // Print the solution
        System.out.print("x = " + x * (c / gcd) +
                       ", y = " + y * (c / gcd) + "\n");
    }
}
  
// Driver Code
public static void main(String[] args) 
{
    int a, b, c;
  
    // Given coefficients
    a = 4;
    b = 18;
    c = 10;
  
    // Function Call
    print_solution(a, b, c);
}
}
  
// This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
  
class GFG{
  
static int x, y; 
  
// Function to implement the extended
// euclid algorithm
static int gcd_extend(int a, int b)
{
      
    // Base Case
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
  
    // Recursively find the gcd
    else
    {
        int g = gcd_extend(b, a % b);
        int x1 = x, y1 = y;
        x = y1;
        y = x1 - (a / b) * y1;
        return g;
    }
}
  
// Function to print the solutions of
// the given equations ax + by = c
static void print_solution(int a, int b, int c)
{
    if (a == 0 && b == 0) 
    {
          
        // Condition for infinite solutions
        if (c == 0)
        {
            Console.Write("Infinite Solutions " +
                          "Exist" + "\n");
        }
  
        // Condition for no solutions exist
        else
        {
            Console.Write("No Solution exists" + 
                          "\n");
        }
    }
    int gcd = gcd_extend(a, b);
  
    // Condition for no solutions exist
    if (c % gcd != 0)
    {
        Console.Write("No Solution exists" + "\n");
    }
    else
    {
          
        // Print the solution
        Console.Write("x = " + x * (c / gcd) +
                    ", y = " + y * (c / gcd) + "\n");
    }
}
  
// Driver Code
public static void Main(String[] args) 
{
    int a, b, c;
  
    // Given coefficients
    a = 4;
    b = 18;
    c = 10;
  
    // Function call
    print_solution(a, b, c);
}
}
  
// This code contributed by amal kumar choubey


输出:
x = -20, y = 5

时间复杂度: O(log(max(A,B))) ,其中A和B是给定线性方程式中x和y的系数。
辅助空间: O(1)