给定整数N , K , A和B ,检查是否有可能通过A到达B ,该循环队列是从1到N的整数循环队列中,通过K长度的跳变来实现的。在可能的情况下,如果可能,则打印“是” 。否则,打印“否” 。
例子:
Input: N = 5, A = 2, B = 1, K = 2
Output: Yes
Explanation: 2 -> 4 -> 1. Therefore, it is possible to reach B from A.
Input: N = 9, A = 6, B = 5, K = 3
Output: No
方法:解决问题的想法是基于以下观察结果:
- 对于位置A ,在t步之后, A的位置为(A + K * t)%N 。
- 对于位置B ,在t步之后, B的位置为(A + K * t)%N 。
- 它可以写成:
(A + K*t) = (N*q + B), where q is any positive integer
(A – B) = N*q – K*t
观察上述方程式(N * q – K * t) ,可将N和K的GCD整除。因此, (A – B)也可以被N和K的GCD整除。因此,要从A到达B , (A – B)必须被GCD(N,K)整除。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return GCD of two
// numbers a and b
int GCD(int a, int b)
{
// Base Case
if (b == 0)
return a;
// Recursively Find the GCD
return GCD(b, a % b);
}
// Function to check of B can be reaced
// from A with a jump of K elements in
// the circular queue
void canReach(int N, int A, int B, int K)
{
// Find GCD of N and K
int gcd = GCD(N, K);
// If A - B is divisible by gcd
// then print Yes
if (abs(A - B) % gcd == 0) {
cout << "Yes";
}
// Otherwise
else {
cout << "No";
}
}
// Driver Code
int main()
{
int N = 5, A = 2, B = 1, K = 2;
// Function Call
canReach(N, A, B, K);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class solution{
// Function to return GCD
// of two numbers a and b
static int GCD(int a, int b)
{
// Base Case
if (b == 0)
return a;
// Recursively Find
// the GCD
return GCD(b, a % b);
}
// Function to check of B can
// be reaced from A with a jump
// of K elements in the circular
// queue
static void canReach(int N, int A,
int B, int K)
{
// Find GCD of N and K
int gcd = GCD(N, K);
// If A - B is divisible
// by gcd then print Yes
if (Math.abs(A - B) %
gcd == 0)
{
System.out.println("Yes");
}
// Otherwise
else
{
System.out.println("No");
}
}
// Driver Code
public static void main(String args[])
{
int N = 5, A = 2,
B = 1, K = 2;
// Function Call
canReach(N, A, B, K);
}
}
// This code is contributed by SURENDRA_GANGWAR
Python3
# Python3 program for the
# above approach
# Function to return GCD
# of 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 check of B
# can be reaced from A
# with a jump of K elements
# in the circular queue
def canReach(N, A, B, K):
# Find GCD of N and K
gcd = GCD(N, K)
# If A - B is divisible
# by gcd then prYes
if (abs(A - B) %
gcd == 0):
print("Yes")
# Otherwise
else:
print("No")
# Driver Code
if __name__ == '__main__':
N = 5
A = 2
B = 1
K = 2
# Function Call
canReach(N, A, B, K)
# This code is contributed by Mohit Kumar 29
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to return GCD
// of two numbers a and b
static int GCD(int a, int b)
{
// Base Case
if (b == 0)
return a;
// Recursively Find
// the GCD
return GCD(b, a % b);
}
// Function to check of B can
// be reaced from A with a jump
// of K elements in the circular
// queue
static void canReach(int N, int A,
int B, int K)
{
// Find GCD of N and K
int gcd = GCD(N, K);
// If A - B is divisible
// by gcd then print Yes
if (Math.Abs(A - B) % gcd == 0)
{
Console.WriteLine("Yes");
}
// Otherwise
else
{
Console.WriteLine("No");
}
}
// Driver Code
public static void Main()
{
int N = 5, A = 2,
B = 1, K = 2;
// Function Call
canReach(N, A, B, K);
}
}
// This code is contributed by code_hunt
输出:
Yes
时间复杂度: O(log(min(N,K))
辅助空间: O(1)