📌  相关文章
📜  检查是否可以通过对两个给定数字进行重复加法或减法来获得N

📅  最后修改于: 2021-04-21 22:17:02             🧑  作者: Mango

给定三个正整数NAB,任务是检查是否有可能通过多次加或减AB来获得N。

例子:

方法:
请按照以下步骤解决问题:

  • 任务是检查是否可以多次加减AB并获得N作为最终结果。
  • 因此,就线性方程而言,可以写成:
    Ax + By = N
    其中xy代表AB相加或相减的次数。负x代表A减去x倍,负y代表B减去y
  • 现在,目的是找到上述方程的积分解。在这里,使用扩展Euclid算法非常有效,该算法表示只有当N%gcd(a,b)为0时解才存在。

下面是上述方法的实现:

C++
// C++ Program to check if
// a number can be obtained
// by repetitive addition
// or subtraction of two numbers
 
#include 
using namespace std;
 
// Function to check and return if
// it is possible to obtain N by
// repetitive addition or subtraction
// of A and B
bool isPossible(int N, int a, int b)
{
    // Calculate GCD of A and B
    int g = __gcd(a, b);
 
    // Condition to check
    // if N can be obtained
    if (N % g == 0)
        return true;
    else
        return false;
}
 
// Driver Code
int main()
{
    int N = 11, a = 2;
    int b = 5;
 
    if (isPossible(N, a, b))
        cout << "YES";
    else
        cout << "NO";
}


Java
// Java program to check if
// a number can be obtained
// by repetitive addition
// or subtraction of two numbers
class GFG{
     
// Recursive function to return
// gcd of a and b
public static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to check and return if
// it is possible to obtain N by
// repetitive addition or subtraction
// of A and B
public static boolean isPossible(int N, int a,
                                 int b)
{
     
    // Calculate GCD of A and B
    int g = gcd(a, b);
     
    // Condition to check
    // if N can be obtained
    if (N % g == 0)
        return true;
    else
        return false;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 11, a = 2;
    int b = 5;
     
    if (isPossible(N, a, b))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to check if
# a number can be obtained
# by repetitive addition
# or subtraction of two numbers
 
# Recursive function to return
# gcd of a and b
def gcd(a, b):
     
    if (b == 0):
        return a
    return gcd(b, a % b)
     
# Function to check and return if
# it is possible to obtain N by
# repetitive addition or subtraction
# of A and B
def isPossible(N, a, b):
     
    # Calculate GCD of A and B
    g = gcd(a, b)
     
    # Condition to check
    # if N can be obtained
    if (N % g == 0):
        return True
    else:
        return False
         
# Driver code
N = 11
a = 2
b = 5
 
if (isPossible(N, a, b) != False):
    print("YES")
else:
    print("NO")
 
# This code is contributed by code_hunt


C#
// C# program to check if
// a number can be obtained
// by repetitive addition
// or subtraction of two numbers
using System;
 
class GFG{
     
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to check and return if
// it is possible to obtain N by
// repetitive addition or subtraction
// of A and B
static bool isPossible(int N, int a,
                              int b)
{
     
    // Calculate GCD of A and B
    int g = gcd(a, b);
     
    // Condition to check
    // if N can be obtained
    if (N % g == 0)
        return true;
    else
        return false;
}
 
// Driver code
public static void Main()
{
    int N = 11, a = 2;
    int b = 5;
     
    if (isPossible(N, a, b))
        Console.Write("YES");
    else
        Console.Write("NO");
}
}
 
// This code is contributed by chitranayal


Javascript


输出:
YES

时间复杂度: O(log(min(A,B))
辅助空间: O(1)