检查 [L, R] 的 GCD 是否可以通过用它们的乘积最多替换 K 次来使 >1
给定一个范围[L, R]和一个整数K ,任务是检查是否有可能使用最多K个操作使给定范围内所有整数的 GCD 大于1 ,其中每个操作替换任意两个整数范围与他们的产品。
例子:
Input: L = 1, R = 5, K = 3 .
Output: Yes
Explanation: All the integers in the given range are {1, 2, 3, 4, 5}. In first operation, replace the first two elements with their product. Hence, the remaining elements become {2, 3, 4, 5}. Similarly, {2, 3, 4, 5} => {2, 3, 20} => {2, 60}. Hence, in three operations the given range can be reduced to {2, 60} such that its GCD is greater than 1.
Input: L = 1, R = 2, K = 0
Output: No
方法:给定的问题可以使用贪心方法来解决。这个想法是在范围内的所有整数中找到最常见的素数,以便其他元素可以与它们合并。可以观察到,在所有情况下最常见的素数都是2 。因此,最佳选择是将所有奇数与其最接近的偶数相乘。因此,如果奇数个数如果大于K ,则有可能否则不会。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to check if it is possible
// to make GCD of all integers in the
// given range more than 1 with the
// help of at most K operations
bool gcdGreaterThanOne(int L, int R, int K)
{
// Case where the range
// has only one integer
if (L == R) {
if (L == 1)
return false;
else
return true;
}
// Stores the count of
// odd integers in [L, R]
int odd = (R - L + 1)
- (R / 2 - ((L - 1) / 2));
return (odd > K) ? false : true;
}
// Driver function
int main()
{
int L = 1;
int R = 5;
int K = 3;
if (gcdGreaterThanOne(L, R, K))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to check if it is possible
// to make GCD of all integers in the
// given range more than 1 with the
// help of at most K operations
static Boolean gcdGreaterThanOne(int L, int R, int K)
{
// Case where the range
// has only one integer
if (L == R) {
if (L == 1)
return false;
else
return true;
}
// Stores the count of
// odd integers in [L, R]
int odd = (R - L + 1)
- (R / 2 - ((L - 1) / 2));
return (odd > K) ? false : true;
}
// Driver function
public static void main (String[] args) {
int L = 1;
int R = 5;
int K = 3;
if (gcdGreaterThanOne(L, R, K))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Function to check if it is possible
# to make GCD of all integers in the
# given range more than 1 with the
# help of at most K operations
def gcdGreaterThanOne(L, R, K):
# Case where the range
# has only one integer
if (L == R):
if (L == 1):
return false
else:
return true
# Stores the count of
# odd integers in [L, R]
odd = (R - L + 1) - (R / 2 - ((L - 1) / 2))
return (odd <= K)
# Driver function
L = 1
R = 5
K = 3
if (gcdGreaterThanOne(L, R, K)):
print("Yes")
else:
print("No")
# This code is contributed by khatriharsh281
C#
// C# implementation for the above approach
using System;
class GFG{
// Function to check if it is possible
// to make GCD of all integers in the
// given range more than 1 with the
// help of at most K operations
static Boolean gcdGreaterThanOne(int L, int R, int K)
{
// Case where the range
// has only one integer
if (L == R) {
if (L == 1)
return false;
else
return true;
}
// Stores the count of
// odd integers in [L, R]
int odd = (R - L + 1)
- (R / 2 - ((L - 1) / 2));
return (odd > K) ? false : true;
}
// Driver code
static public void Main (){
int L = 1;
int R = 5;
int K = 3;
if (gcdGreaterThanOne(L, R, K))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by sanjoy_62.
Javascript
Yes
时间复杂度: O(1)
辅助空间: O(1)