📌  相关文章
📜  检查一个数字是否可以表示为两个完美幂的和

📅  最后修改于: 2021-05-04 10:57:12             🧑  作者: Mango

给定正数N ,任务是检查给定数N是否可以a x + b y的形式表示 其中x和y> 1且a和b>0。如果N可以给定形式表示,则输出true,否则输出false

例子:

方法:这个想法是使用完美能力的概念来确定总和是否存在。步骤如下:

  1. 创建一个数组(例如perfectPower [] )以存储是否为完美幂的数字。
  2. 现在,数组perfectPower []存储了所有构成完美幂的元素,因此我们生成了该数组中所有元素的所有可能的对和。
  3. 将上面步骤中计算出的总和的标记保留在数组isSum []中,因为它可以以a x + b y的形式表示 
  4. 完成上述步骤后,如果isSum [N]为true,则输出true,否则输出false

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function that returns true if n
// can be written as a^m+b^n
bool isSumOfPower(int n)
{
    // Taking isSum boolean array
    // for check the sum exist or not
    bool isSum[n + 1];
  
    // To store perfect squares
    vector perfectPowers;
  
    perfectPowers.push_back(1);
  
    for (int i = 0; i < (n + 1); i++) {
  
        // Initally all sums as false
        isSum[i] = false;
    }
  
    for (long long int i = 2;
         i < (n + 1); i++) {
  
        if (isSum[i] == true) {
  
            // If sum exist then push
            // that sum into perfect
            // square vector
            perfectPowers.push_back(i);
            continue;
        }
  
        for (long long int j = i * i;
             j > 0 && j < (n + 1);
             j *= i) {
            isSum[j] = true;
        }
    }
  
    // Mark all perfect powers as false
    for (int i = 0;
         i < perfectPowers.size(); i++) {
        isSum[perfectPowers[i]] = false;
    }
  
    // Traverse each perfectPowers
    for (int i = 0;
         i < perfectPowers.size(); i++) {
  
        for (int j = i;
             j < perfectPowers.size(); j++) {
  
            // Calculating Sum with
            // perfect powers array
            int sum = perfectPowers[i]
                      + perfectPowers[j];
  
            if (sum < (n + 1))
                isSum[sum] = true;
        }
    }
    return isSum[n];
}
  
// Driver Code
int main()
{
    // Given Number n
    int n = 9;
  
    // Function Call
    if (isSumOfPower(n)) {
        cout << "true\n";
    }
    else {
        cout << "false\n";
    }
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function that returns true if n
// can be written as a^m+b^n
static boolean isSumOfPower(int n)
{
      
    // Taking isSum boolean array
    // for check the sum exist or not
    boolean []isSum = new boolean[n + 1];
  
    // To store perfect squares
    Vector perfectPowers = new Vector();
  
    perfectPowers.add(1);
  
    for(int i = 0; i < (n + 1); i++)
    {
          
        // Initally all sums as false
        isSum[i] = false;
    }
  
    for(int i = 2; i < (n + 1); i++)
    {
        if (isSum[i] == true)
        {
              
            // If sum exist then push
            // that sum into perfect
            // square vector
            perfectPowers.add(i);
            continue;
        }
  
        for(int j = i * i; 
                j > 0 && j < (n + 1); 
                j *= i)
        {
            isSum[j] = true;
        }
    }
  
    // Mark all perfect powers as false
    for(int i = 0;
            i < perfectPowers.size(); 
            i++)
    {
        isSum[perfectPowers.get(i)] = false;
    }
  
    // Traverse each perfectPowers
    for(int i = 0; 
            i < perfectPowers.size();
            i++) 
    {
        for(int j = i; 
                j < perfectPowers.size(); 
                j++)
        {
              
            // Calculating Sum with
            // perfect powers array
            int sum = perfectPowers.get(i) + 
                      perfectPowers.get(j);
  
            if (sum < (n + 1))
                isSum[sum] = true;
        }
    }
    return isSum[n];
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given number n
    int n = 9;
  
    // Function call
    if (isSumOfPower(n))
    {
        System.out.print("true\n");
    }
    else 
    {
        System.out.print("false\n");
    }
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
  
# Function that returns true if n
# can be written as a^m+b^n
def isSumOfPower(n):
      
    # Taking isSum boolean array
    # for check the sum exist or not
    isSum = [0] * (n + 1)
  
    # To store perfect squares
    perfectPowers = []
  
    perfectPowers.append(1)
  
    for i in range(n + 1):
  
        # Initally all sums as false
        isSum[i] = False
      
    for i in range(2, n + 1):
        if (isSum[i] == True):
  
            # If sum exist then push
            # that sum into perfect
            # square vector
            perfectPowers.append(i)
            continue
          
        j = i * i
        while(j > 0 and j < (n + 1)):
            isSum[j] = True
            j *= i
          
    # Mark all perfect powers as false
    for i in range(len(perfectPowers)):
        isSum[perfectPowers[i]] = False
      
    # Traverse each perfectPowers
    for i in range(len(perfectPowers)):
        for j in range(len(perfectPowers)):
  
            # Calculating Sum with
            # perfect powers array
            sum = (perfectPowers[i] + 
                   perfectPowers[j])
  
            if (sum < (n + 1)):
                isSum[sum] = True
          
    return isSum[n]
  
# Driver Code
  
# Given Number n
n = 9
  
# Function call
if (isSumOfPower(n)):
    print("true")
else:
    print("false")
  
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function that returns true if n
// can be written as a^m+b^n
static bool isSumOfPower(int n)
{
      
    // Taking isSum bool array
    // for check the sum exist or not
    bool []isSum = new bool[n + 1];
  
    // To store perfect squares
    List perfectPowers = new List();
  
    perfectPowers.Add(1);
  
    for(int i = 0; i < (n + 1); i++)
    {
          
        // Initally all sums as false
        isSum[i] = false;
    }
  
    for(int i = 2; i < (n + 1); i++)
    {
        if (isSum[i] == true)
        {
              
            // If sum exist then push
            // that sum into perfect
            // square vector
            perfectPowers.Add(i);
            continue;
        }
  
        for(int j = i * i; 
                j > 0 && j < (n + 1); 
                j *= i)
        {
            isSum[j] = true;
        }
    }
  
    // Mark all perfect powers as false
    for(int i = 0;
            i < perfectPowers.Count; 
            i++)
    {
        isSum[perfectPowers[i]] = false;
    }
  
    // Traverse each perfectPowers
    for(int i = 0; 
            i < perfectPowers.Count;
            i++) 
    {
        for(int j = i; 
                j < perfectPowers.Count; 
                j++)
        {
              
            // Calculating Sum with
            // perfect powers array
            int sum = perfectPowers[i] + 
                      perfectPowers[j];
  
            if (sum < (n + 1))
                isSum[sum] = true;
        }
    }
    return isSum[n];
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given number n
    int n = 9;
  
    // Function call
    if (isSumOfPower(n))
    {
        Console.Write("true\n");
    }
    else
    {
        Console.Write("false\n");
    }
}
}
  
// This code is contributed by amal kumar choubey


输出:
true

时间复杂度: O(N)
辅助空间: O(N)