📌  相关文章
📜  计数直到 C 的数字,可以通过添加或减去 A 或 B 减少到 0

📅  最后修改于: 2021-10-26 07:02:55             🧑  作者: Mango

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

例子:

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

  • 考虑分别执行AB 的XY数的加法或减法。
  • 在对任何数字N应用操作后,它变为Ax + By 。因此,通过扩展欧几里德算法,可以说存在整数系数xy ,使得Ax + By = GCD(A, B)
  • 因此, N必须是GCD(A, B)的倍数,比如G 。现在问题简化为找到G[1, C]范围内的倍数,即 floor (C / G)

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

  • 找到AB的 GCD 并将其存储在一个变量中,比如G
  • 现在, [1, C]范围内的数字计数是G的倍数,其值至多为C ,由floor (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


Javascript


输出:
5

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