📜  线性丢番图方程

📅  最后修改于: 2021-04-28 18:00:45             🧑  作者: Mango

Diophantine方程是一个多项式方程,通常包含两个或多个未知数,因此仅需要积分解。积分解决方案是一种解决方案,使得所有未知变量仅采用整数值。
给定三个整数a,b,c,它们表示形式为:ax + by = c的线性方程。确定方程式是否具有使x和y均为整数值的解。
例子 :

Input : a = 3, b = 6, c = 9
Output: Possible
Explanation : The Equation turns out to be, 
3x + 6y = 9 one integral solution would be 
x = 1 , y = 1

Input : a = 3, b = 6, c = 8
Output : Not Possible
Explanation : o integral values of x and y 
exists that can satisfy the equation 3x + 6y = 8

Input : a = 2, b = 5, c = 1
Output : Possible
Explanation : Various integral solutions
possible are, (-2,1) , (3,-1) etc.

解决方案:
对于线性丢番图方程组,当且仅当两个变量的系数的GCD完美地划分常数项时,存在积分解。换句话说,如果GCD(a,b)除以c,则存在积分解。
因此,确定方程是否具有积分解的算法非常简单。

  • 查找a和b的GCD
  • 检查c%GCD(a,b)== 0
  • 如果是,则打印可能
  • 无法进行其他打印

下面是上述方法的实现。

C++
// C++ program to check for solutions of diophantine
// equations
#include 
using namespace std;
 
//utility function to find the GCD of two numbers
int gcd(int a, int b)
{
    return (a%b == 0)? abs(b) : gcd(b,a%b);
}
 
// This function checks if integral solutions are
// possible
bool isPossible(int a, int b, int c)
{
    return (c%gcd(a,b) == 0);
}
 
//driver function
int main()
{
    // First example
    int a = 3, b = 6, c = 9;
    isPossible(a, b, c)? cout << "Possible\n" :
                        cout << "Not Possible\n";
 
    // Second example
    a = 3, b = 6, c = 8;
    isPossible(a, b, c)? cout << "Possible\n" :
                       cout << "Not Possible\n";
 
    // Third example
    a = 2, b = 5, c = 1;
    isPossible(a, b, c)? cout << "Possible\n" :
                       cout << "Not Possible\n";
 
    return 0;
}


Java
// Java program to check for solutions of
// diophantine equations
import java.io.*;
 
class GFG {
     
    // Utility function to find the GCD
    // of two numbers
    static int gcd(int a, int b)
    {
        return (a % b == 0) ?
                Math.abs(b) : gcd(b,a%b);
    }
     
    // This function checks if integral
    // solutions are possible
    static boolean isPossible(int a,
                            int b, int c)
    {
        return (c % gcd(a, b) == 0);
    }
     
    // Driver function
    public static void main (String[] args)
    {
        // First example
        int a = 3, b = 6, c = 9;
        if(isPossible(a, b, c))
            System.out.println( "Possible" );
        else
            System.out.println( "Not Possible");
     
        // Second example
        a = 3; b = 6; c = 8;
        if(isPossible(a, b, c))
            System.out.println( "Possible") ;
        else
            System.out.println( "Not Possible");
     
        // Third example
        a = 2; b = 5; c = 1;
        if(isPossible(a, b, c))
            System.out.println( "Possible" );
        else
            System.out.println( "Not Possible");
    }
}
 
// This code is contributed by anuj_67.


Python3
# Python 3 program to check for solutions
# of diophantine equations
from math import gcd
 
# This function checks if integral
# solutions are possible
def isPossible(a, b, c):
    return (c % gcd(a, b) == 0)
 
# Driver Code
if __name__ == '__main__':
     
    # First example
    a = 3
    b = 6
    c = 9
    if (isPossible(a, b, c)):
        print("Possible")
    else:
        print("Not Possible")
 
    # Second example
    a = 3
    b = 6
    c = 8
    if (isPossible(a, b, c)):
        print("Possible")
    else:
        print("Not Possible")
 
    # Third example
    a = 2
    b = 5
    c = 1
    if (isPossible(a, b, c)):
        print("Possible")
    else:
        print("Not Possible")
         
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to check for
// solutions of diophantine
// equations
using System;
 
class GFG
{
     
    // Utility function to find
    // the GCD of two numbers
    static int gcd(int a, int b)
    {
        return (a % b == 0) ?
                Math.Abs(b) :
               gcd(b, a % b);
    }
     
    // This function checks
    // if integral solutions
    // are possible
    static bool isPossible(int a,
                           int b,
                           int c)
    {
        return (c % gcd(a, b) == 0);
    }
     
    // Driver Code
    public static void Main ()
    {
        // First example
        int a = 3, b = 6, c = 9;
        if(isPossible(a, b, c))
            Console.WriteLine("Possible");
        else
            Console.WriteLine("Not Possible");
     
        // Second example
        a = 3; b = 6; c = 8;
        if(isPossible(a, b, c))
            Console.WriteLine("Possible") ;
        else
            Console.WriteLine("Not Possible");
     
        // Third example
        a = 2; b = 5; c = 1;
        if(isPossible(a, b, c))
            Console.WriteLine("Possible");
        else
            Console.WriteLine("Not Possible");
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

Possible
Not Possible
Possible