Encuentra las raíces integrales de una ecuación cúbica dada

Dados 5 enteros, digamos A, B, C, D y E, que representan la ecuación cúbica  f(x) = A*x^{3} + B*x^{2} + C*x + D = E  , la tarea es encontrar la solución integral para esta ecuación. Si no existe ninguna solución integral, imprima «NA» .
Ejemplos: 
 

Entrada: A = 1, B = 0, C = 0, D = 0, E = 27 
Salida: 3
Entrada: A = 1, B = 0, C = 0, D = 0, E = 16 
Salida: NA 
 

Enfoque: La idea es utilizar la búsqueda binaria . A continuación se muestran los pasos: 
 

  1. Inicialice la variable inicial y final como 0 y 10 5 respectivamente.
  2. Encuentre el valor medio (digamos medio ) de inicio y fin, verifique si satisface la ecuación dada o no.
  3. Si la corriente media satisface la ecuación dada, imprime el valor medio.
  4. De lo contrario, si el valor de f(x) es menor que E , la actualización comienza como mid + 1 .
  5. De lo contrario, la actualización finaliza a mediados de – 1 .
  6. Si no podemos encontrar ninguna solución integral para la ecuación anterior, imprima «-1» .

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the value at x of
// the given equation
long long int check(int A, int B, int C,
                    int D, long long int x)
{
 
    long long int ans;
 
    // Find the value equation at x
    ans = (A * x * x * x
           + B * x * x
           + C * x
           + D);
 
    // Return the value of ans
    return ans;
}
 
// Function to find the integral
// solution of the given equation
void findSolution(int A, int B, int C,
                  int D, int E)
{
 
    // Initialise start and end
    int start = 0, end = 100000;
 
    long long int mid, ans;
 
    // Implement Binary Search
    while (start <= end) {
 
        // Find mid
        mid = start + (end - start) / 2;
 
        // Find the value of f(x) using
        // current mid
        ans = check(A, B, C, D, mid);
 
        // Check if current mid satisfy
        // the equation
        if (ans == E) {
 
            // Print mid and return
            cout << mid << endl;
            return;
        }
 
        if (ans < E)
            start = mid + 1;
        else
            end = mid - 1;
    }
 
    // Print "NA" if not found
    // any integral solution
    cout << "NA";
}
 
// Driver Code
int main()
{
    int A = 1, B = 0, C = 0;
    int D = 0, E = 27;
 
    // Function Call
    findSolution(A, B, C, D, E);
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the value at x of
// the given equation
static long check(int A, int B, int C,
                         int D, long x)
{
    long ans;
 
    // Find the value equation at x
    ans = (A * x * x * x +
           B * x * x + C * x + D);
 
    // Return the value of ans
    return ans;
}
 
// Function to find the integral
// solution of the given equation
static void findSolution(int A, int B, int C,
                         int D, int E)
{
 
    // Initialise start and end
    long start = 0, end = 100000;
 
    long mid, ans;
 
    // Implement Binary Search
    while (start <= end)
    {
 
        // Find mid
        mid = start + (end - start) / 2;
 
        // Find the value of f(x) using
        // current mid
        ans = check(A, B, C, D, mid);
 
        // Check if current mid satisfy
        // the equation
        if (ans == E)
        {
 
            // Print mid and return
            System.out.println(mid);
            return;
        }
 
        if (ans < E)
            start = mid + 1;
        else
            end = mid - 1;
    }
 
    // Print "NA" if not found
    // any integral solution
    System.out.println("NA");
}
 
// Driver Code
public static void main(String args[])
{
    int A = 1, B = 0, C = 0;
    int D = 0, E = 27;
 
    // Function Call
    findSolution(A, B, C, D, E);
}
}
 
// This code is contributed by Code_Mech

Python3

# Python3 program for the above approach
 
# Function to find the value at x of
# the given equation
def check(A, B, C, D, x) :
 
    ans = 0;
 
    # Find the value equation at x
    ans = (A * x * x * x +
           B * x * x + C * x + D);
 
    # Return the value of ans
    return ans;
 
# Function to find the integral
# solution of the given equation
def findSolution(A, B, C, D, E) :
 
    # Initialise start and end
    start = 0; end = 100000;
 
    mid = 0;
    ans = 0;
 
    # Implement Binary Search
    while (start <= end) :
 
        # Find mid
        mid = start + (end - start) // 2;
 
        # Find the value of f(x) using
        # current mid
        ans = check(A, B, C, D, mid);
 
        # Check if current mid satisfy
        # the equation
        if (ans == E) :
 
            # Print mid and return
            print(mid);
            return;
 
        if (ans < E) :
            start = mid + 1;
        else :
            end = mid - 1;
 
    # Print "NA" if not found
    # any integral solution
    print("NA");
 
# Driver Code
if __name__ == "__main__" :
 
    A = 1; B = 0; C = 0;
    D = 0; E = 27;
 
    # Function Call
    findSolution(A, B, C, D, E);
     
# This code is contributed by AnkitRai01

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to find the value at x of
// the given equation
static long check(int A, int B, int C,
                         int D, long x)
{
    long ans;
 
    // Find the value equation at x
    ans = (A * x * x * x +
           B * x * x + C * x + D);
 
    // Return the value of ans
    return ans;
}
 
// Function to find the integral
// solution of the given equation
static void findSolution(int A, int B, int C,
                         int D, int E)
{
 
    // Initialise start and end
    long start = 0, end = 100000;
 
    long mid, ans;
 
    // Implement Binary Search
    while (start <= end)
    {
 
        // Find mid
        mid = start + (end - start) / 2;
 
        // Find the value of f(x) using
        // current mid
        ans = check(A, B, C, D, mid);
 
        // Check if current mid satisfy
        // the equation
        if (ans == E)
        {
 
            // Print mid and return
            Console.WriteLine(mid);
            return;
        }
 
        if (ans < E)
            start = mid + 1;
        else
            end = mid - 1;
    }
 
    // Print "NA" if not found
    // any integral solution
    Console.Write("NA");
}
 
// Driver Code
public static void Main()
{
    int A = 1, B = 0, C = 0;
    int D = 0, E = 27;
 
    // Function Call
    findSolution(A, B, C, D, E);
}
}
 
// This code is contributed by Code_Mech

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to find the value at x of
// the given equation
function check(A, B, C, D, x)
{
    var ans;
 
    // Find the value equation at x
    ans = (A * x * x * x +
           B * x * x + C * x + D);
 
    // Return the value of ans
    return ans;
}
 
// Function to find the integral
// solution of the given equation
function findSolution(A, B, C, D, E)
{
 
    // Initialise start and end
    var start = 0, end = 100000;
 
    var mid, ans;
 
    // Implement Binary Search
    while (start <= end)
    {
 
        // Find mid
        mid = parseInt(start + (end - start) / 2);
 
        // Find the value of f(x) using
        // current mid
        ans = check(A, B, C, D, mid);
 
        // Check if current mid satisfy
        // the equation
        if (ans == E)
        {
 
            // Print mid and return
            document.write(mid);
            return;
        }
 
        if (ans < E)
            start = mid + 1;
        else
            end = mid - 1;
    }
 
    // Print "NA" if not found
    // any integral solution
    document.write("NA");
}
 
// Driver Code
var A = 1, B = 0, C = 0;
var D = 0, E = 27;
 
// Function Call
findSolution(A, B, C, D, E);
 
// This code is contributed by Ankita saini
 
</script>
Producción: 

3

 

Complejidad de tiempo: O (log N)
 

Publicación traducida automáticamente

Artículo escrito por grand_master y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *