给定两个正整数’a’和’b’,它们表示方程ax + by = m的系数。对于x和y的任何正整数,找到满足方程式的m的最小值。在此最小值之后,该方程由m的所有(更大)值满足。如果不存在这样的最小值,则返回“ -1”。
例子:
Input: a = 4, b = 7
Output: 18
Explanation: 18 is the smallest value that can
can be satisfied by equation 4x + 7y.
4*1 + 7*2 = 18
And after 18 all values are satisifed
4*3 + 7*1 = 19
4*5 + 7*0 = 20
... and so on.
这是Frobenius硬币问题的一种变体。在Frobenius硬币问题中,我们需要找到两个硬币无法表示的最大数字。面额为“ a”和“ b”的硬币的最大金额为a * b –(a + b)。因此可以使用两个硬币表示的最小数字,也可以表示之后的所有数字是a * b –(a + b)+ 1。
一个重要的情况是’a’和’b’的GCD不为1。例如,如果’a’= 4并且’b’= 6,则可以使用两个硬币表示的所有值都是偶数(或可以对方程进行分层的m是偶数。因此,所有不是2的倍数的值都不能满足该方程。在这种情况下,没有最小值,之后所有值都满足方程式。
下面是上述想法的实现:
C++
// C++ program to find the minimum value of m that satisfies
// ax + by = m and all values after m also satisfy
#include
using namespace std;
int findMin(int a, int b)
{
// If GCD is not 1, then there is no such value,
// else value is obtained using "a*b-a-b+1'
return (__gcd(a, b) == 1)? a*b-a-b+1 : -1;
}
// Driver code
int main()
{
int a = 4, b = 7;
cout << findMin(a, b) << endl;
return 0;
}
Java
// Java program to find the
// minimum value of m that
// satisfies ax + by = m
// and all values after m
// also satisfy
import java.io.*;
class GFG
{
// Recursive function to
// return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0 && b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater$
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int findMin( int a, int b)
{
// If GCD is not 1, then
// there is no such value,
// else value is obtained
// using "a*b-a-b+1'
return (__gcd(a, b) == 1)?
a * b - a - b + 1 : -1;
}
// Driver code
public static void main (String[] args)
{
int a = 4;
int b = 7;
System.out.println(findMin(a, b));
}
}
// This code is contributed
// by akt_mit
Python3
# Python3 program to find the minimum
# value of m that satisfies ax + by = m
# and all values after m also satisfy
# Recursive function to return
# gcd of a and b
def __gcd(a, b):
# Everything divides 0
if (a == 0 or b == 0):
return 0;
# base case
if (a == b):
return a;
# a is greater
if (a > b):
return __gcd(a - b, b);
return __gcd(a, b - a);
def findMin( a, b):
# If GCD is not 1, then
# there is no such value,
# else value is obtained
# using "a*b-a-b+1'
if(__gcd(a, b) == 1):
return (a * b - a - b + 1)
else:
return -1
# Driver code
a = 4;
b = 7;
print(findMin(a, b));
# This code is contributed by mits
C#
// C# program to find the minimum
// value of m that satisfies
// ax + by = m and all values
// after m also satisfy
class GFG
{
// Recursive function to
// return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0 && b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater$
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int findMin( int a, int b)
{
// If GCD is not 1, then
// there is no such value,
// else value is obtained
// using "a*b-a-b+1'
return (__gcd(a, b) == 1)?
a * b - a - b + 1 : -1;
}
// Driver code
public static void Main()
{
int a = 4;
int b = 7;
System.Console.WriteLine(findMin(a, b));
}
}
// This code is contributed
// by mits
PHP
$b)
return __gcd($a - $b, $b);
return __gcd($a, $b - $a);
}
function findMin( $a, $b)
{
// If GCD is not 1, then
// there is no such value,
// else value is obtained
// using "a*b-a-b+1'
return (__gcd($a, $b) == 1)?
$a * $b - $a - $b + 1 : -1;
}
// Driver code
$a = 4; $b = 7;
echo findMin($a, $b) ;
// This code is contributed by anuj_67.
?>
Javascript
输出:
18