📌  相关文章
📜  通过任意次数加或减 A、B 或 0 形成 C 之前的不同值的计数

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

通过任意次数加或减 A、B 或 0 形成 C 之前的不同值的计数

给定三个整数A、B 和 C。您可以添加或减去 A、B 或 0 任意次数以形成0 < final_value ≤ C范围内的新值。任务是找到可能的这种不同最终值的计数。

例子

方法:这个想法是使用A 和 B的 GCD g

上述方法有效,因为每个不同的可能值都是xA+yB

  • 如果A可以写成g×a, B可以写成g×b
  • 然后,所需的最终值可以写为xAg+yBg = (x*g*a + y*g*b) = g*(xa+yb)
  • 由于可能的最大值是 C,因此C = g*(xa+yb)
  • 因此,可能的此类值的计数 = C/g ,这是必需的答案。

以下是上述方法的实现

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate gcd
int gcd(int A, int B)
{
    if (B == 0)
        return A;
    else
        return gcd(B, A % B);
}
 
// Function to find number of possible final values
int getDistinctValues(int A, int B, int C)
{
     
    // Find the gcd of two numbers
    int g = gcd(A, B);
 
    // Calculate number of distinct values
    int num_values = C / g;
 
    // Return values
    return num_values;
}
 
// Driver Code
int main()
{
    int A = 2;
    int B = 3;
    int C = 10;
 
    cout << (getDistinctValues(A, B, C));
    return 0;
}
 
// This code is contributed by subhammahato348


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
    // Function to calculate gcd
    static int gcd(int A, int B)
    {
        if (B == 0)
            return A;
        else
            return gcd(B, A % B);
    }
 
    // Function to find number of possible final values
    static int getDistinctValues(int A, int B, int C)
    {
 
        // Find the gcd of two numbers
        int g = gcd(A, B);
 
        // Calculate number of distinct values
        int num_values = C / g;
 
        // Return values
        return num_values;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 2;
        int B = 3;
        int C = 10;
 
        System.out.println(getDistinctValues(A, B, C));
    }
}


Python3
# Python program for the above approach
 
# Function to calculate gcd
def gcd(A, B) :
     
    if (B == 0) :
        return A;
    else :
        return gcd(B, A % B);
 
# Function to find number of possible final values
def getDistinctValues(A, B, C) :
     
    # Find the gcd of two numbers
    g = gcd(A, B);
 
    # Calculate number of distinct values
    num_values = C / g;
 
    # Return values
    return int(num_values);
 
# Driver Code
A = 2;
B = 3;
C = 10;
 
print(getDistinctValues(A, B, C));
 
# This code is contributed by target_2.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to calculate gcd
  static int gcd(int A, int B)
  {
    if (B == 0)
      return A;
    else
      return gcd(B, A % B);
  }
 
  // Function to find number of possible final values
  static int getDistinctValues(int A, int B, int C)
  {
 
    // Find the gcd of two numbers
    int g = gcd(A, B);
 
    // Calculate number of distinct values
    int num_values = C / g;
 
    // Return values
    return num_values;
  }
 
 
  // Driver code
  static void Main()
  {
    int A = 2;
    int B = 3;
    int C = 10;
 
    Console.Write(getDistinctValues(A, B, C));
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
10

时间复杂度:O(log(max(A, B))

空间复杂度:O(1)