给定无限长度的数组以及两个互为质数的整数M和N ,任务是查找从arr [i]或arr [ i + M]或arr [i + N] 。请注意,结果始终是有限的。
例子:
Input: M = 2, N = 5
Output: 2
From index 0, the indices that can be visited are
0 + 2 = 2
0 + 2 + 2 = 4
0 + 5 = 5
0 + 2 + 2 + 2 = 6
0 + 2 + 5 = 7
0 + 2 + 2 + 2 + 2 = 8
0 + 2 + 2 + 5 = 9
0 + 5 + 5 = 10
…
1 and 3 are the only indices that cannot be visited.
Input: M = 5, N = 6
Output: 15
方法:
- 使用Frobenius数(例如X =(M * N)– M – N)找到使用M & N的任何组合都无法获得的最大索引。
- 由于X是无法访问的最大索引,因此不需要检查每个大于X的索引。
- 现在,对于小于X的索引,如果X是未访问的,则Y = X – M和Z = 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