给定整数N 、 K 、 A和B ,检查是否有可能通过K长度的跳转在从1到N的整数的循环队列中从A到达B顺序放置。在每一步中,如果可能,则打印“是” 。否则,打印“否” 。
例子:
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
Javascript
输出:
Yes
时间复杂度: O(log(min(N, K))
辅助空间: O(1)