📜  给定二次方程的最小根,其值大于等于K

📅  最后修改于: 2021-04-21 21:12:28             🧑  作者: Mango

给定二次方程式F(x)= Ax 2 + Bx + C的常数分别为A,B和C,以及一个整数K ,任务是找到根x的最小值,以使F(x)≥K 。如果不存在这样的值,则打印“ -1” 。假定F(x)是一个单调递增的函数。

例子:

方法:想法是使用二进制搜索来找到x的最小值。步骤如下:

  • 要获得等于或大于K的值, x的值必须在[1,sqrt(K)]范围内因为这是二次方程。
  • 现在,基本上需要在范围内搜索适当的元素,因此可以执行此二进制搜索。
  • 计算F(mid) ,其中mid是范围[1,sqrt(K)]的中间值。现在,可能出现以下三种情况:
    • 如果F(mid)≥K && F(mid)这意味着当前的mid是必需的答案。
    • 如果F(mid)这意味着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)