📜  求非线性方程2X + 5Y = N的积分解

📅  最后修改于: 2021-06-25 10:56:32             🧑  作者: Mango

给定一个表示形式为2 X + 5 Y = N的非线性方程的整数N ,任务是找到一个满足给定方程的积分对( XY )。如果存在多个解决方案,则打印其中任何一种。否则,打印-1

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个变量,说xMax来存储X的最大可能值。
  • 更新xMaxlog 2 N。
  • 初始化一个变量,例如yMax,以存储Y的最大可能值。
  • 更新yMax记录5 N
  • 遍历XY的所有可能的值和对于XY的每一个值和对于每一对,判断是否满足给定的方程或没有。如果发现是正确的,则打印XY的相应值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
     
     
#include 
using namespace std;
 
 
// Function to find the value
// of power(X, N)
long long power(long long x,
                long long N)
{
     
    // Stores the value
    // of (X ^ N)
    long long res = 1;
     
     
    // Calculate the value of
    // power(x, N)
    while (N > 0) {
     
     
        // If N is odd
        if (N & 1) {
     
     
            // Update res
            res = (res * x) ;
        }
     
     
        // Update x
        x = (x * x) ;
     
     
        // Update N
        N = N >> 1;
    }
    return res;
}
 
 
 
// Function to find the value of
// X and Y that satisfy the condition
void findValX_Y(long long N)
{
     
 
    // Base Case
    if (N <= 1) {
    cout<<-1<


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
    
// Function to find the value
// of power(X, N)
static int power(int x, int N)
{
     
    // Stores the value
    // of (X ^ N)
    int res = 1;
     
    // Calculate the value of
    // power(x, N)
    while (N > 0)
    {
         
        // If N is odd
        if ((N & 1) != 0)
        {
             
            // Update res
            res = (res * x);
        }
         
        // Update x
        x = (x * x);
         
        // Update N
        N = N >> 1;
    }
    return res;
}
 
// Function to find the value of
// X and Y that satisfy the condition
static void findValX_Y(int N)
{
     
    // Base Case
    if (N <= 1)
    {
        System.out.println(-1);
        return;
    }
 
    // Stores maximum possible
    // of X.
    int xMax;
     
    // Update xMax
    xMax = (int)Math.log(N);
     
    // Stores maximum possible
    // of Y.
    int yMax;
     
    // Update yMax
    yMax = (int)(Math.log(N) / Math.log(5.0));
     
    // Iterate over all possible
    // values of X
    for(int i = 1; i <= xMax; i++)
    {
         
        // Iterate over all possible
        // values of Y
        for(int j = 1; j <= yMax; j++)
        {
             
            // Stores value of 2^i
            int a = power(2, i);
             
            // Stores value of 5^j
            int b = power(5, j);
                 
            // If the pair (i, j)
            // satisfy the equation
            if (a + b == N)
            {
                System.out.print(i + " " + j);
                return;
            }
        }
    }
         
    // If no solution exists
    System.out.println("-1");
}
     
// Driver Code
public static void main(String args[])
{
    int N = 129;
     
    findValX_Y(N);
}
}
 
// This code is contributed by bgangwar59


Python3
# Python3 program to implement
# the above approach
from math import log2
       
# Function to find the value
# of power(X, N)
def power(x, N):
     
    # Stores the value
    # of (X ^ N)
    res = 1
  
    # Calculate the value of
    # power(x, N)
    while (N > 0):
         
        # If N is odd
        if (N & 1):
             
            # Update res
            res = (res * x)
             
        # Update x
        x = (x * x)
         
        # Update N
        N = N >> 1
         
    return res
  
# Function to find the value of
# X and Y that satisfy the condition
def findValX_Y(N):
     
    # Base Case
    if (N <= 1):
       print(-1)
       return
    
    # Stores maximum possible
    # of X
    xMax = 0
     
    # Update xMax
    xMax = int(log2(N))
     
    # Stores maximum possible
    # of Y
    yMax = 0
     
    # Update yMax
    yMax = int(log2(N) / log2(5.0))
     
    # Iterate over all possible
    # values of X
    for i in range(1, xMax + 1):
         
        # Iterate over all possible
        # values of Y
        for j in range(1, yMax + 1):
             
            # Stores value of 2^i
            a = power(2, i)
             
            # Stores value of 5^j
            b = power(5, j)
             
            # If the pair (i, j)
            # satisfy the equation
            if (a + b == N):
                print(i, j)
                return
  
    # If no solution exists
    print(-1)
  
# Driver Code
if __name__ == '__main__':
     
    N = 129
  
    findValX_Y(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
    
// Function to find the
// value of power(X, N)
static int power(int x,
                 int N)
{   
  // Stores the value
  // of (X ^ N)
  int res = 1;
 
  // Calculate the value
  // of power(x, N)
  while (N > 0)
  {        
    // If N is odd
    if ((N & 1) != 0)
    {
 
      // Update res
      res = (res * x);
    }
 
    // Update x
    x = (x * x);
 
    // Update N
    N = N >> 1;
  }
  return res;
}
 
// Function to find the
// value of X and Y that
// satisfy the condition
static void findValX_Y(int N)
{    
  // Base Case
  if (N <= 1)
  {
    Console.WriteLine(-1);
    return;
  }
 
  // Stores maximum
  // possible of X.
  int xMax;
 
  // Update xMax
  xMax = (int)Math.Log(N);
 
  // Stores maximum possible
  // of Y.
  int yMax;
 
  // Update yMax
  yMax = (int)(Math.Log(N) /
               Math.Log(5.0));
 
  // Iterate over all possible
  // values of X
  for(int i = 1; i <= xMax; i++)
  {
    // Iterate over all possible
    // values of Y
    for(int j = 1; j <= yMax; j++)
    {
      // Stores value of 2^i
      int a = power(2, i);
 
      // Stores value of 5^j
      int b = power(5, j);
 
      // If the pair (i, j)
      // satisfy the equation
      if (a + b == N)
      {
        Console.Write(i + " " + j);
        return;
      }
    }
  }
 
  // If no solution exists
  Console.WriteLine("-1");
}
 
// Driver Code
public static void Main()
{
  int N = 129;
  findValX_Y(N);
}
}
 
// This code is contributed by surendra_gangwar


Javascript


输出:

2 3

时间复杂度: O(log 2 N * log 5 N)
辅助空间: O(1)