给定三个非负整数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
方法:可以根据以下观察结果解决给定问题:
- 考虑X和Y分别执行A和B的加法或减法数。
- 在对任意数字N进行运算之后,它变为Ax + By 。因此,通过扩展欧几里得算法,可以说存在整数系数x和y ,使得Ax + By = GCD(A,B) 。
- 因此, N必须是GCD(A,B)的倍数,例如G。现在,问题减少到找到G的倍数,该倍数在[1,C]范围内,即下限(C / G) 。
请按照以下步骤解决问题:
- 找到A和B的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)