给定三个非负整数A 、 B和C ,任务是计算范围[1, C]中可以通过添加或减去A或B减少到 0 的数字。
例子:
Input: A = 2, B = 4, C = 7
Output: 3
Explanation: The numbers from the range [1, 7] that can be reduced to 0 by given operations are:
- For element 2: The number can be modified as 2 – 2 = 0.
- For element 4: The number can be modified as 4 – 2 – 2 = 0.
- For element 6: The number can be modified as 6 – 4 – 2 = 0.
Therefore, the total count is 3.
Input: A = 2, B = 3, C = 5
Output: 5
方法:根据以下观察可以解决给定的问题:
- 考虑分别执行A和B 的X和Y数的加法或减法。
- 在对任何数字N应用操作后,它变为Ax + By 。因此,通过扩展欧几里德算法,可以说存在整数系数x和y ,使得Ax + By = GCD(A, B) 。
- 因此, N必须是GCD(A, B)的倍数,比如G 。现在问题简化为找到G在[1, C]范围内的倍数,即 floor (C / G) 。
请按照以下步骤解决问题:
- 找到A和B的 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)