给定整数k , d1 , d2和整数数组arr [] 。从数字k开始,您可以进行大小为d1和d2的跳跃,即,从k开始的所有可能移动都是:
- k + d1和k – d1
- k + d2和k – d2
任务是从k进行任意数量的跳转,找到任意数组中的数字,并且给定的跳转大小为d1或d2 。对于每个数字,如果可到达则打印1 ,否则打印0 。
例子:
Input: k = 10, d1 = 4, d2 = 6, arr[] = {10, 15, 20}
Output: 1 0 1
10 can be reached from k with no extra move.
20 can be reached with k + d1 + d2 = 10 + 4 + 6 = 20
Input: k = 8, d1 = 3, d2 = 2, arr[] = {9, 4}
Output: 1 1
方法:从k可以跳到d1或d2的任何数字x都将具有x = k +(i * d1)+(j * d2)的形式,其中i和j是整数。
令d1和d2的GCD为gcd 。由于gcd同时划分了d1和d2 。因此我们可以写成d1 = m1 * gcd和d2 = m2 * gcd其中m1和m2是整数
并且x = k + gcd *(i * m1 + j * m2)= k + M * gcd 。
因此,从k可获得的任何数字x都应满足(x – k)%gcd = 0 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
using namespace std;
// Function that returns the vector containing the
// result for the reachability of the required numbers
void reachTheNums(int nums[], int k, int d1, int d2, int n)
{
int i, ans[n] = { 0 };
int gcd = __gcd(d1, d2);
for (i = 0; i < n; i++) {
int x = nums[i] - k;
// If distance x is coverable
if (x % gcd == 0)
ans[i] = 1;
else
ans[i] = 0;
}
for (i = 0; i < n; i++)
cout << ans[i] << " ";
}
// Driver code
int main()
{
// Numbers to be checked for reachability
int nums[] = { 9, 4 };
int n = sizeof(nums) / sizeof(nums[0]);
// Starting number K
int k = 8;
// Sizes of jumps d1 and d2
int d1 = 3, d2 = 2;
reachTheNums(nums, k, d1, d2, n);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function that returns the vector containing the
// result for the reachability of the required numbers
static void reachTheNums(int nums[], int k, int d1, int d2, int n)
{
int i;
int ans[] = new int[n];
int gcd = __gcd(d1, d2);
for (i = 0; i < n; i++) {
int x = nums[i] - k;
// If distance x is coverable
if (x % gcd == 0)
ans[i] = 1;
else
ans[i] = 0;
}
for (i = 0; i < n; i++)
System.out.print(ans[i] + " ");
}
// Driver code
public static void main (String[] args) {
// Numbers to be checked for reachability
int nums[] = { 9, 4 };
int n =nums.length;
// Starting number K
int k = 8;
// Sizes of jumps d1 and d2
int d1 = 3, d2 = 2;
reachTheNums(nums, k, d1, d2, n);
}
}
// This code is contributed by inder_verma..
Python3
# Python3 implementation of the approach
import math as mt
# Function that returns the vector
# containing the result for the reachability
# of the required numbers
def reachTheNums(nums, k, d1, d2, n):
ans = [0 for i in range(n)]
gcd = mt.gcd(d1, d2)
for i in range(n):
x = nums[i] - k
# If distance x is coverable
if (x % gcd == 0):
ans[i] = 1
else:
ans[i] = 0
for i in range(n):
print(ans[i], end = " ")
# Driver code
# Numbers to be checked for
# reachability
nums = [9, 4]
n = len(nums)
# Starting number K
k = 8
# Sizes of jumps d1 and d2
d1, d2 = 3, 2
reachTheNums(nums, k, d1, d2, n)
# This code is conteibuted
# by mohit kumar 29
C#
// C# implementation of the above approach
using System ;
class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function that returns the vector containing the
// result for the reachability of the required numbers
static void reachTheNums(int []nums, int k, int d1, int d2, int n)
{
int i;
int []ans = new int[n];
int gcd = __gcd(d1, d2);
for (i = 0; i < n; i++) {
int x = nums[i] - k;
// If distance x is coverable
if (x % gcd == 0)
ans[i] = 1;
else
ans[i] = 0;
}
for (i = 0; i < n; i++)
Console.Write(ans[i] + " ");
}
// Driver code
public static void Main () {
// Numbers to be checked for reachability
int []nums = { 9, 4 };
int n =nums.Length;
// Starting number K
int k = 8;
// Sizes of jumps d1 and d2
int d1 = 3, d2 = 2;
reachTheNums(nums, k, d1, d2, n);
}
// This code is contributed by Ryuga
}
PHP
Javascript
1 1
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。