📜  找出对(a,b)使得Aa + Bb = N

📅  最后修改于: 2021-04-21 21:26:18             🧑  作者: Mango

给定三个整数NA B的任务是找到一对正整数(a,b)使得A a + B b = N。如果不存在这样的对,则打印-1

例子:

方法:想法是计算log A Nlog B N并检查每对(i,j) (0≤i≤log A N和0≤j≤log B N), A i + B j是否相等到N。请按照以下步骤解决问题:

  • 首先,找到大于N的A和B的最小幂,并将其分别存储在变量XY中
  • 检查每个 (i,j) ,使得0≤i≤X0≤j≤Y 如果A i + B j等于N ,则打印对(i,j)
  • 如果没有找到这样的对,则打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the minimum
// power of A and B greater than N
int power(long long int A, long long int N)
{
    // Stores the power of A which
    // is greater than N
    int count = 0;
    if (A == 1)
        return 0;
 
    while (N) {
 
        // Increment count by 1
        count++;
 
        // Divide N by A
        N /= A;
    }
    return count;
}
 
// Function to find a pair (a, b)
// such that A^a + B^b = N
void Pairs(long long int N, long long int A,
           long long int B)
{
    int powerA, powerB;
 
    // Calculate the minimum power
    // of A greater than N
    powerA = power(A, N);
 
    // Calculate the minimum power
    // of B greater than N
    powerB = power(B, N);
 
    // Make copy of A and B
    long long int intialB = B, intialA = A;
 
    // Traverse for every pair (i, j)
    A = 1;
    for (int i = 0; i <= powerA; i++) {
 
        B = 1;
        for (int j = 0; j <= powerB; j++) {
 
            // Check if B^j + A^i = N
            // To overcome the overflow problem
            // use B=N-A rather than B+A=N
            if (B == N - A) {
                cout << i << " " << j << endl;
                return;
            }
 
            // Increment power B by 1
            B *= intialB;
        }
 
        // Increment power A by 1
        A *= intialA;
    }
 
    // Finally print -1 if no pair
    // is found
    cout << -1 << endl;
    return;
}
 
// Driver Code
int main()
{
 
    // Given A, B and N
    long long int N = 106, A = 3, B = 5;
 
    // Function Call
    Pairs(N, A, B);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to calculate the minimum
// power of A and B greater than N
static int power(int A, int N)
{
     
    // Stores the power of A which
    // is greater than N
    int count = 0;
     
    if (A == 1)
        return 0;
 
    while (N > 0)
    {
         
        // Increment count by 1
        count++;
 
        // Divide N by A
        N /= A;
    }
    return count;
}
 
// Function to find a pair (a, b)
// such that A^a + B^b = N
static void Pairs(int N, int A, int B)
{
    int powerA, powerB;
 
    // Calculate the minimum power
    // of A greater than N
    powerA = power(A, N);
 
    // Calculate the minimum power
    // of B greater than N
    powerB = power(B, N);
 
    // Make copy of A and B
    int intialB = B, intialA = A;
 
    // Traverse for every pair (i, j)
    A = 1;
    for(int i = 0; i <= powerA; i++)
    {
         
        B = 1;
        for(int j = 0; j <= powerB; j++)
        {
             
            // Check if B^j + A^i = N
            // To overcome the overflow problem
            // use B=N-A rather than B+A=N
            if (B == N - A)
            {
                System.out.println(i + " " + j);
                return;
            }
 
            // Increment power B by 1
            B *= intialB;
        }
 
        // Increment power A by 1
        A *= intialA;
    }
 
    // Finally print -1 if no pair
    // is found
    System.out.println("-1");
    return;
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given A, B and N
    int N = 106, A = 3, B = 5;
 
    // Function Call
    Pairs(N, A, B);
}
}
 
// This code is contributed by 18bhupenderyadav18


Python3
# Python program for the above approach
 
# Function to calculate the minimum
# power of A and B greater than N
def power(A, N):
   
    # Stores the power of A which
    # is greater than N
    count = 0;
    if (A == 1):
        return 0;
    while (N > 0):
       
        # Increment count by 1
        count += 1;
 
        # Divide N by A
        N //= A;
    return int(count);
 
# Function to find a pair (a, b)
# such that A^a + B^b = N
def Pairs(N, A, B):
    powerA, powerB = 0, 0;
 
    # Calculate the minimum power
    # of A greater than N
    powerA = power(A, N);
 
    # Calculate the minimum power
    # of B greater than N
    powerB = power(B, N);
 
    # Make copy of A and B
    intialB = B;
    intialA = A;
 
    # Traverse for every pair (i, j)
    A = 1;
    for i in range(powerA + 1):
        B = 1;
        for j in range(powerB + 1):
 
            # Check if B^j + A^i = N
            # To overcome the overflow problem
            # use B=N-A rather than B+A=N
            if (B == N - A):
                print(i , " " , j);
                return;
 
            # Increment power B by 1
            B *= intialB;
 
        # Increment power A by 1
        A *= intialA;
 
    # Finally pr-1 if no pair
    # is found
    print("-1");
    return;
 
# Driver Code
if __name__ == '__main__':
 
  # Given A, B and N
    N = 106;
    A = 3;
    B = 5;
 
    # Function Call
    Pairs(N, A, B);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG
{
     
// Function to calculate the minimum
// power of A and B greater than N
static int power(int A, int N)
{
     
    // Stores the power of A which
    // is greater than N
    int count = 0;
     
    if (A == 1)
        return 0;
    while (N > 0)
    {
         
        // Increment count by 1
        count++;
 
        // Divide N by A
        N /= A;
    }
    return count;
}
 
// Function to find a pair (a, b)
// such that A^a + B^b = N
static void Pairs(int N, int A, int B)
{
    int powerA, powerB;
 
    // Calculate the minimum power
    // of A greater than N
    powerA = power(A, N);
 
    // Calculate the minimum power
    // of B greater than N
    powerB = power(B, N);
 
    // Make copy of A and B
    int intialB = B, intialA = A;
 
    // Traverse for every pair (i, j)
    A = 1;
    for(int i = 0; i <= powerA; i++)
    {
         
        B = 1;
        for(int j = 0; j <= powerB; j++)
        {
             
            // Check if B^j + A^i = N
            // To overcome the overflow problem
            // use B=N-A rather than B+A=N
            if (B == N - A)
            {
                Console.WriteLine(i + " " + j);
                return;
            }
 
            // Increment power B by 1
            B *= intialB;
        }
 
        // Increment power A by 1
        A *= intialA;
    }
 
    // Finally print -1 if no pair
    // is found
    Console.WriteLine("-1");
    return;
}
 
// Driver Code
public static void Main(String []args)
{
     
    // Given A, B and N
    int N = 106, A = 3, B = 5;
 
    // Function Call
    Pairs(N, A, B);
}
}
 
// This code is contributed by 29AjayKumar


输出:
4 2

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