📌  相关文章
📜  在D上执行给定操作后,数组中可到达的元素数

📅  最后修改于: 2021-04-22 09:17:13             🧑  作者: Mango

给定一个数组arr []和三个整数DAB。您可以从数字D开始,并且可以随时在当前数字上加上或减去A或B。这意味着您可以多次执行以下四个操作:

  1. A添加到当前号码。
  2. 从当前数字中减去A。
  3. B添加到当前号码。
  4. 从当前数字中减去B。

该任务是从给定数组中找到执行上述操作后可以达到的整数计数。

例子:

方法:可以使用双色子方程的性质来解决此问题

令我们要从数组中得出的整数为x 。如果我们以D开头并且可以对AB进行任意次数的加/减运算,则意味着我们需要确定以下方程式是否具有整数解。

如果它在pq中具有整数解,则意味着我们可以从D到达整数x ,否则就没有。
重新排列此等式到

当且仅当(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