📜  查找无限数组中未访问索引的数量

📅  最后修改于: 2021-04-30 03:38:07             🧑  作者: Mango

给定一个无限长的数组以及两个互为质数的整数MN ,任务是查找从arr [i]arr [ i + M]arr [i + N]请注意,结果始终是有限的。

例子

方法:

  • 使用Frobenius数(例如X =(M * N)– M – N)找到使用MN的任何组合都无法获得的最大索引。
  • 由于X是无法访问的最大索引,因此不需要检查每个大于X的索引。
  • 现在,对于小于X的索引,如果X是未访问的,则Y = X – MZ = X – N分别也unrechable和同样如此ÿ – Mž – N和等..直到指数大于0

下面是上述方法的实现:

C++
// C++ implementation of the approach 
#include 
using namespace std;
  
// Function to return the count 
// of unvisited indices starting 
// from the index 0 
int countUnvisited(int n, int m)
{
      
    // Largest index that 
    // cannot be visited 
    int X = (m * n) - m - n; 
  
    // Push the index to the queue 
    queue queue;
  
    queue.push(X); 
  
    // To store the required count 
    int count = 0; 
    while (queue.size() > 0) 
    { 
  
        // Current index that cannot be visited 
        int curr = queue.front(); 
        queue.pop();
  
        // Increment the count for 
        // the current index 
        count++; 
  
        // (curr - m) and (curr - n) are also 
        // unreachable if they are valid indices 
        if (curr - m > 0) 
            queue.push(curr - m); 
        if (curr - n > 0) 
            queue.push(curr - n); 
    } 
  
    // Return the required count 
    return count; 
}
  
// Driver code 
int main()
{
    int n = 2, m = 5; 
  
    cout << countUnvisited(n, m);
  
    return 0;
}
  
// This code is contributed by Sanjit_Prasad


Java
// Java implementation of the approach
import java.util.LinkedList;
import java.util.Queue;
  
class GFG {
  
    // Function to return the count
    // of unvisited indices starting
    // from the index 0
    public static int countUnvisited(int n, int m)
    {
  
        // Largest index that
        // cannot be visited
        int X = (m * n) - m - n;
  
        // Push the index to the queue
        Queue queue = new LinkedList<>();
        queue.add(X);
  
        // To store the required count
        int count = 0;
        while (!queue.isEmpty()) {
  
            // Current index that cannot be visited
            int curr = queue.poll();
  
            // Increment the count for
            // the current index
            count++;
  
            // (curr - m) and (curr - n) are also
            // unreachable if they are valid indices
            if (curr - m > 0)
                queue.add(curr - m);
            if (curr - n > 0)
                queue.add(curr - n);
        }
  
        // Return the required count
        return count;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = 2, m = 5;
        System.out.print(countUnvisited(n, m));
    }
}


Python 3
# Python 3 implementation of the approach 
  
# Function to return the count 
# of unvisited indices starting 
# from the index 0 
def countUnvisited(n, m):
      
    # Largest index that 
    # cannot be visited 
    i = 0
    X = (m * n) - m - n
  
    # Push the index to the queue 
    queue = []
  
    queue.append(X)
  
    # To store the required count 
    count = 0
    while (len(queue) > 0):
          
        # Current index that cannot be visited 
        curr = queue[0] 
        queue.remove(queue[0])
  
        # Increment the count for 
        # the current index 
        count += 1
  
        # (curr - m) and (curr - n) are also 
        # unreachable if they are valid indices 
        if (curr - m > 0):
            queue.append(curr - m) 
        if (curr - n > 0):
            queue.append(curr - n) 
  
    # Return the required count 
    return count
  
# Driver code 
if __name__ == '__main__':
    n = 2
    m = 5
  
    print(countUnvisited(n, m))
      
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the above approach 
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    // Function to return the count
    // of unvisited indices starting
    // from the index 0
    public static int countUnvisited(int n, int m)
    {
  
        // Largest index that
        // cannot be visited
        int X = (m * n) - m - n;
  
        // Push the index to the queue
        Queue queue = new Queue();
        queue.Enqueue(X);
  
        // To store the required count
        int count = 0;
        while (queue.Count != 0) 
        {
  
            // Current index that cannot be visited
            int curr = queue.Dequeue();
  
            // Increment the count for
            // the current index
            count++;
  
            // (curr - m) and (curr - n) are also
            // unreachable if they are valid indices
            if (curr - m > 0)
                queue.Enqueue(curr - m);
            if (curr - n > 0)
                queue.Enqueue(curr - n);
        }
  
        // Return the required count
        return count;
    }
  
    // Driver code
    public static void Main(String []args)
    {
        int n = 2, m = 5;
        Console.WriteLine(countUnvisited(n, m));
    }
}
  
// This code is contributed by PrinciRaj1992


输出:
2