📜  发现所有提高到K的对都相差N

📅  最后修改于: 2021-04-17 18:05:13             🧑  作者: Mango

给定两个正整数XK ,任务是找到所有可能的整数对(A,B) ,以使提高到幂K的整数对之间的差为给定的整数X。如果不存在这样的对,则打印“ -1”
注意: K的值至少为5X的最大值为10 18

例子:

方法:给定的问题可以基于X的最大可能值为10 18的观察来解决。因此,整数对(A,B)的值将在[-1000,1000]范围内。请按照以下步骤解决问题:

  • 初始化一个变量,例如count0 ,以计算满足给定条件的对数。
  • 生成范围为[-1000,1000]的所有可能的对(A,B) ,如果(A K – B K )的值为X ,则打印相应的对并以1递增计数
  • 完成上述步骤后,如果count的值为0 ,则打印“ -1”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print pairs whose
// difference raised to the power K is X
void ValidPairs(int X, int K)
{
    // Stores the count of valid pairs
    long long int count = 0;
 
    // Iterate over the range [-1000, 1000]
    for (int A = -1000; A <= 1000; A++) {
 
        // Iterate over the range [-1000, 1000]
        for (int B = -1000; B <= 1000; B++) {
 
            // If the current pair satisfies
            // the given condition
            if (pow(A, K) - pow(B, K) == X) {
 
                // Increment the count by 1
                count++;
                cout << A << " " << B << endl;
            }
        }
    }
 
    // If no such pair exists
    if (count == 0) {
        cout << "-1";
    }
}
 
// Driver Code
int main()
{
    long long int X = 33;
    int K = 5;
    ValidPairs(X, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to print pairs whose
// difference raised to the power K is X
static void ValidPairs(int X, int K)
{
     
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterate over the range [-1000, 1000]
    for(int A = -1000; A <= 1000; A++)
    {
         
        // Iterate over the range [-1000, 1000]
        for(int B = -1000; B <= 1000; B++)
        {
             
            // If the current pair satisfies
            // the given condition
            if (Math.pow(A, K) - Math.pow(B, K) == X)
            {
                 
                // Increment the count by 1
                count++;
                System.out.println(A + " " + B );
            }
        }
    }
 
    // If no such pair exists
    if (count == 0)
    {
        System.out.println("-1");
    }
}
 
// Driver Code
public static void main(String args[])
{
    int X = 33;
    int K = 5;
     
    ValidPairs(X, K);
}
}
 
// This code is contributed by souravghosh0416


Python3
# Python program for the above approach
 
# Function to prpairs whose
# difference raised to the power K is X
def ValidPairs(X, K) :
     
    # Stores the count of valid pairs
    count = 0
 
    # Iterate over the range [-1000, 1000]
    for A in range(-1000, 1001, 1):
 
        # Iterate over the range [-1000, 1000]
        for B in range(-1000, 1001, 1):
 
            # If the current pair satisfies
            # the given condition
            if (pow(A, K) - pow(B, K) == X) :
 
                # Increment the count by 1
                count += 1
                print(A, B)
             
    # If no such pair exists
    if (count == 0) :
        cout << "-1"
     
# Driver Code
X = 33
K = 5
ValidPairs(X, K)
 
# This code is contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to print pairs whose
// difference raised to the power K is X
static void ValidPairs(int X, int K)
{
 
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterate over the range [-1000, 1000]
    for(int A = -1000; A <= 1000; A++)
    {
 
        // Iterate over the range [-1000, 1000]
        for(int B = -1000; B <= 1000; B++)
        {
             
            // If the current pair satisfies
            // the given condition
            if (Math.Pow(A, K) - Math.Pow(B, K) == X)
            {
                 
                // Increment the count by 1
                count++;
                Console.WriteLine(A + " " + B);
            }
        }
    }
 
    // If no such pair exists
    if (count == 0)
    {
        Console.WriteLine("-1");
    }
}
 
// Driver Code
static public void Main()
{
    int X = 33;
    int K = 5;
 
    ValidPairs(X, K);
}
}
 
// This code is contributed by Dharanendra L V.


Javascript


输出:
1 -2
2 -1

时间复杂度: O(2000 * 2000)
辅助空间: O(1)