给定一个数组arr []和三个整数D , A和B。您可以从数字D开始,并且可以随时在当前数字上加上或减去A或B。这意味着您可以多次执行以下四个操作:
- 将A添加到当前号码。
- 从当前数字中减去A。
- 将B添加到当前号码。
- 从当前数字中减去B。
该任务是从给定数组中找到执行上述操作后可以达到的整数计数。
例子:
Input: arr[] = {4, 5, 6, 7, 8, 9}, D = 4, A = 4, B = 6
Output: 3
The reachable numbers are:
4 = 4
6 = 4 + 6 – 4
8 = 4 + 4
Input: arr[] = {24, 53, 126, 547, 48, 97}, D = 2, A = 5, B = 8
Output: 6
方法:可以使用双色子方程的性质来解决此问题
令我们要从数组中得出的整数为x 。如果我们以D开头并且可以对A或B进行任意次数的加/减运算,则意味着我们需要确定以下方程式是否具有整数解。
D + p * A + q * B = x
如果它在p和q中具有整数解,则意味着我们可以从D到达整数x ,否则就没有。
重新排列此等式到
p * A + q * B = x – D
当且仅当(x-D)%GCD(A,B)= 0时,该方程式才具有整数解。
现在遍历数组中的整数,并检查该方程是否对当前x有解。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the GCD
// of a and b
int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
// Function to return the count of reachable
// integers from the given array
int findReachable(int arr[], int D, int A,
int B, int n)
{
// GCD of A and B
int gcd_AB = GCD(A, B);
// To store the count of reachable integers
int count = 0;
for (int i = 0; i < n; i++) {
// If current element can be reached
if ((arr[i] - D) % gcd_AB == 0)
count++;
}
// Return the count
return count;
}
// Driver code
int main()
{
int arr[] = { 4, 5, 6, 7, 8, 9 };
int n = sizeof(arr) / sizeof(int);
int D = 4, A = 4, B = 6;
cout << findReachable(arr, D, A, B, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the GCD
// of a and b
static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
// Function to return the count of reachable
// integers from the given array
static int findReachable(int[] arr, int D, int A,
int B, int n)
{
// GCD of A and B
int gcd_AB = GCD(A, B);
// To store the count of reachable integers
int count = 0;
for (int i = 0; i < n; i++)
{
// If current element can be reached
if ((arr[i] - D) % gcd_AB == 0)
count++;
}
// Return the count
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 4, 5, 6, 7, 8, 9 };
int n = arr.length;
int D = 4, A = 4, B = 6;
System.out.println(findReachable(arr, D, A, B, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python implementation of the approach
# Function to return the GCD
# of a and b
def GCD(a, b):
if (b == 0):
return a;
return GCD(b, a % b);
# Function to return the count of reachable
# integers from the given array
def findReachable(arr, D, A, B, n):
# GCD of A and B
gcd_AB = GCD(A, B);
# To store the count of reachable integers
count = 0;
for i in range(n):
# If current element can be reached
if ((arr[i] - D) % gcd_AB == 0):
count+=1;
# Return the count
return count;
# Driver code
arr = [ 4, 5, 6, 7, 8, 9 ];
n = len(arr);
D = 4; A = 4; B = 6;
print(findReachable(arr, D, A, B, n));
# This code is contributed by 29AjayKumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the GCD
// of a and b
static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
// Function to return the count of reachable
// integers from the given array
static int findReachable(int[] arr, int D, int A,
int B, int n)
{
// GCD of A and B
int gcd_AB = GCD(A, B);
// To store the count of reachable integers
int count = 0;
for (int i = 0; i < n; i++)
{
// If current element can be reached
if ((arr[i] - D) % gcd_AB == 0)
count++;
}
// Return the count
return count;
}
// Driver code
public static void Main()
{
int []arr = { 4, 5, 6, 7, 8, 9 };
int n = arr.Length;
int D = 4, A = 4, B = 6;
Console.WriteLine(findReachable(arr, D, A, B, n));
}
}
// This code is contributed by AnkitRai01
输出:
3