📌  相关文章
📜  计数最多C的数字,通过加或减A或B可以将其减少为0

📅  最后修改于: 2021-04-17 14:06:25             🧑  作者: Mango

给定三个非负整数ABC ,任务是计算范围[1,C]中的数字,这些数字可以通过加或减AB来减少为0。

例子:

方法:可以根据以下观察结果解决给定问题:

  • 考虑XY分别执行AB的加法或减法数。
  • 在对任意数字N进行运算之后,它变为Ax + By 。因此,通过扩展欧几里得算法,可以说存在整数系数xy ,使得Ax + By = GCD(A,B)
  • 因此, N必须是GCD(A,B)的倍数,例如G。现在,问题减少到找到G的倍数,该倍数在[1,C]范围内,即下限(C / G)

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

  • 找到AB的GCD并将其存储在变量中,例如G。
  • 现在,范围[1,C]上的数字计数是G的倍数,其值最多为C ,由下限(C / G)给出

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate GCD of the
// two numbers a and b
long long gcd(long long a, long long b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Recursively find the GCD
    return gcd(b, a % b);
}
 
// Function to count the numbers up
// to C that can be reduced to 0 by
// adding or subtracting A or B
void countDistinctNumbers(long long A,
                          long long B,
                          long long C)
{
    // Stores GCD of A and B
    long long g = gcd(A, B);
 
    // Stores the count of multiples
    // of g in the range (0, C]
    long long count = C / g;
 
    // Print the result
    cout << count;
}
 
// Driver Code
int main()
{
    long long A = 2, B = 3, C = 5;
    countDistinctNumbers(A, B, C);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to calculate GCD of the
// two numbers a and b
static long gcd(long a, long b)
{
     
    // Base Case
    if (b == 0)
        return a;
 
    // Recursively find the GCD
    return gcd(b, a % b);
}
 
// Function to count the numbers up
// to C that can be reduced to 0 by
// adding or subtracting A or B
static void countDistinctNumbers(long A, long B,
                                 long C)
{
     
    // Stores GCD of A and B
    long g = gcd(A, B);
 
    // Stores the count of multiples
    // of g in the range (0, C]
    long count = C / g;
 
    // Print the result
    System.out.println(count);
}
 
// Driver Code
public static void main(String[] args)
{
    long A = 2, B = 3, C = 5;
     
    countDistinctNumbers(A, B, C);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to calculate GCD of the
# two numbers a and b
def gcd(a, b):
     
    # Base Case
    if (b == 0):
        return a
         
    # Recursively find the GCD
    return gcd(b, a % b)
 
# Function to count the numbers up
# to C that can be reduced to 0 by
# adding or subtracting A or B
def countDistinctNumbers(A, B, C):
     
    # Stores GCD of A and B
    g = gcd(A, B)
     
    # Stores the count of multiples
    # of g in the range (0, C]
    count = C // g
  
    # Print the result
    print(count)
 
# Driver code
A = 2
B = 3
C = 5
 
countDistinctNumbers(A, B, C)
 
# This code is contributed by abhinavjain194


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to calculate GCD of the
// two numbers a and b
static long gcd(long a, long b)
{
     
    // Base Case
    if (b == 0)
        return a;
 
    // Recursively find the GCD
    return gcd(b, a % b);
}
 
// Function to count the numbers up
// to C that can be reduced to 0 by
// adding or subtracting A or B
static void countDistinctNumbers(long A, long B,
                                 long C)
{
     
    // Stores GCD of A and B
    long g = gcd(A, B);
 
    // Stores the count of multiples
    // of g in the range (0, C]
    long count = C / g;
 
    // Print the result
    Console.Write(count);
}
 
// Driver Code
static void Main()
{
    long A = 2, B = 3, C = 5;
    countDistinctNumbers(A, B, C);
}
}
 
// This code is contributed by abhinavjain194


输出:
5

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