给定二次方程F(x) = Ax 2 + Bx + C的常数为A、B 和 C以及整数K ,任务是找到根x的最小值,使得F(x) ≥ K 。如果不存在这样的值,则打印“-1” 。假设F(x)是一个单调递增的函数。
例子:
Input: A = 3, B = 4, C = 5, K = 6
Output: 1
Explanation:
For the given values F(x) = 3x2 + 4x + 5 the minimum value of x is 1, F(x) = 12, which is greater than the given value of K.
Input: A = 3, B = 4, C = 5, K = 150
Output: 7
Explanation:
For the given values F(x) = 3x2 + 4x + 5 the minimum value of x is 7, F(x) = 180, which is greater than the given value of K.
方法:想法是使用二分搜索来找到x的最小值。以下是步骤:
- 要获得等于或大于K的值, x的值必须在[1, sqrt(K)]范围内,因为这是一个二次方程。
- 现在,基本上需要在范围内搜索适当的元素,因此实现了这种二分搜索。
- 计算F(mid) ,其中mid是范围[1, sqrt(K)]的中间值。现在可能出现以下三种情况:
- 如果F(mid) ≥ K && F(mid) < K:这意味着当前的 mid 是所需的答案。
- 如果F(mid) < K:这意味着 mid 的当前值小于 x 的要求值。因此,向右移动,即在后半部分,因为F(x)是一个递增函数。
- 如果F(mid) > K:这意味着 mid 的当前值大于 x 的所需值。所以,向左移动,即前半部分为F(x)是一个递增函数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate value of
// quadratic equation for some x
int func(int A, int B, int C, int x)
{
return (A * x * x + B * x + C);
}
// Function to calculate the minimum
// value of x such that F(x) >= K using
// binary search
int findMinx(int A, int B, int C, int K)
{
// Start and end value for
// binary search
int start = 1;
int end = ceil(sqrt(K));
// Binary Search
while (start <= end) {
int mid = start + (end - start) / 2;
// Computing F(mid) and F(mid-1)
int x = func(A, B, C, mid);
int Y = func(A, B, C, mid - 1);
// Checking the three cases
// If F(mid) >= K and
// F(mid-1) < K return mid
if (x >= K && Y < K) {
return mid;
}
// If F(mid) < K go to mid+1 to end
else if (x < K) {
start = mid + 1;
}
// If F(mid) > K go to start to mid-1
else {
end = mid - 1;
}
}
// If no such value exist
return -1;
}
// Driver Code
int main()
{
// Given coefficients of Equations
int A = 3, B = 4, C = 5, K = 6;
// Find minimum value of x
cout << findMinx(A, B, C, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to calculate value of
// quadratic equation for some x
static int func(int A, int B, int C, int x)
{
return (A * x * x + B * x + C);
}
// Function to calculate the minimum
// value of x such that F(x) >= K using
// binary search
static int findMinx(int A, int B, int C, int K)
{
// Start and end value for
// binary search
int start = 1;
int end = (int)Math.ceil(Math.sqrt(K));
// Binary Search
while (start <= end)
{
int mid = start + (end - start) / 2;
// Computing F(mid) and F(mid-1)
int x = func(A, B, C, mid);
int Y = func(A, B, C, mid - 1);
// Checking the three cases
// If F(mid) >= K and
// F(mid-1) < K return mid
if (x >= K && Y < K)
{
return mid;
}
// If F(mid) < K go to mid+1 to end
else if (x < K)
{
start = mid + 1;
}
// If F(mid) > K go to start to mid-1
else
{
end = mid - 1;
}
}
// If no such value exist
return -1;
}
// Driver code
public static void main(String[] args)
{
// Given coefficients of Equations
int A = 3, B = 4, C = 5, K = 6;
// Find minimum value of x
System.out.println(findMinx(A, B, C, K));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
import math
# Function to calculate value of
# quadratic equation for some x
def func(A, B, C, x):
return (A * x * x + B * x + C)
# Function to calculate the minimum
# value of x such that F(x) >= K using
# binary search
def findMinx(A, B, C, K):
# Start and end value for
# binary search
start = 1
end = math.ceil(math.sqrt(K))
# Binary Search
while (start <= end):
mid = start + (end - start) // 2
# Computing F(mid) and F(mid-1)
x = func(A, B, C, mid)
Y = func(A, B, C, mid - 1)
# Checking the three cases
# If F(mid) >= K and
# F(mid-1) < K return mid
if (x >= K and Y < K):
return mid
# If F(mid) < K go to mid+1 to end
elif (x < K):
start = mid + 1
# If F(mid) > K go to start to mid-1
else:
end = mid - 1
# If no such value exist
return -1
# Driver Code
# Given coefficients of Equations
A = 3
B = 4
C = 5
K = 6
# Find minimum value of x
print(findMinx(A, B, C, K))
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate value of
// quadratic equation for some x
static int func(int A, int B, int C, int x)
{
return (A * x * x + B * x + C);
}
// Function to calculate the minimum
// value of x such that F(x) >= K using
// binary search
static int findMinx(int A, int B, int C, int K)
{
// Start and end value for
// binary search
int start = 1;
int end = (int)Math.Ceiling(Math.Sqrt(K));
// Binary Search
while (start <= end)
{
int mid = start + (end - start) / 2;
// Computing F(mid) and F(mid-1)
int x = func(A, B, C, mid);
int Y = func(A, B, C, mid - 1);
// Checking the three cases
// If F(mid) >= K and
// F(mid-1) < K return mid
if (x >= K && Y < K)
{
return mid;
}
// If F(mid) < K go to mid+1 to end
else if (x < K)
{
start = mid + 1;
}
// If F(mid) > K go to start to mid-1
else
{
end = mid - 1;
}
}
// If no such value exist
return -1;
}
// Driver code
public static void Main(String[] args)
{
// Given coefficients of Equations
int A = 3, B = 4, C = 5, K = 6;
// Find minimum value of x
Console.WriteLine(findMinx(A, B, C, K));
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
1
时间复杂度: O(log(sqrt(K))
辅助空间: O(1)