📌  相关文章
📜  数为N的不同幂,使得总和等于K

📅  最后修改于: 2021-06-26 22:54:31             🧑  作者: Mango

给定两个数字NK ,任务是打印N的不同次幂,这些次幂用于获得总和K。如果不可能,请打印-1

例子:

观察:对于任意一个a ,都需要观察到的一个观察结果是:如果从0到k-1的所有幂都最多使用一次幂相加,那么就不可能有一个大于K的数。
示例:令a = 3且K =4。然后:

幼稚的方法:通过上述观察,可以形成幼稚的方法。这个想法是连续地从K中减去不超过KN的最高幂,直到K达到0。如果在任何情况下,K等于先前已从中减去的某个幂,那么就不可能得到等于的总和。因此,使用数组来跟踪从K中减去的幂。

下面是上述方法的实现:

C++
// C++ implementation to find distinct
// powers of N that add upto K
  
#include 
using namespace std;
  
// Function to return the highest power
// of N not exceeding K
int highestPower(int n, int k)
{
    int i = 0;
    int a = pow(n, i);
  
    // Loop to find the highest power
    // less than K
    while (a <= k) {
        i += 1;
        a = pow(n, i);
    }
    return i - 1;
}
  
// Initializing the PowerArray
// with all 0's.
int b[50] = { 0 };
  
// Function to print
// the distinct powers of N
// that add upto K
int PowerArray(int n, int k)
{
    while (k) {
  
        // Getting the highest
        // power of n before k
        int t = highestPower(n, k);
  
        // To check if the power
        // is being used twice or not
        if (b[t]) {
  
            // Print -1 if power
            // is being used twice
            cout << -1;
            return 0;
        }
  
        else
            // If the power is not visited,
            // then mark the power as visited
            b[t] = 1;
  
        // Decrementing the value of K
        k -= pow(n, t);
    }
  
    // Printing the powers of N
    // that sum up to K
    for (int i = 0; i < 50; i++) {
        if (b[i]) {
            cout << i << ", ";
        }
    }
}
  
// Driver code
int main()
{
    int N = 3;
    int K = 40;
    PowerArray(N, K);
    return 0;
}


Java
// Java implementation to find distinct
// powers of N that add upto K
  
class GFG{
   
// Function to return the highest power
// of N not exceeding K
static int highestPower(int n, int k)
{
    int i = 0;
    int a = (int) Math.pow(n, i);
   
    // Loop to find the highest power
    // less than K
    while (a <= k) {
        i += 1;
        a = (int) Math.pow(n, i);
    }
    return i - 1;
}
   
// Initializing the PowerArray
// with all 0's.
static int b[] = new int[50];
   
// Function to print
// the distinct powers of N
// that add upto K
static int PowerArray(int n, int k)
{
    while (k>0) {
   
        // Getting the highest
        // power of n before k
        int t = highestPower(n, k);
   
        // To check if the power
        // is being used twice or not
        if (b[t]>0) {
   
            // Print -1 if power
            // is being used twice
            System.out.print(-1);
            return 0;
        }
   
        else
            // If the power is not visited,
            // then mark the power as visited
            b[t] = 1;
   
        // Decrementing the value of K
        k -= Math.pow(n, t);
    }
   
    // Printing the powers of N
    // that sum up to K
    for (int i = 0; i < 50; i++) {
        if (b[i] > 0) {
            System.out.print(i+ ", ");
        }
    }
    return 0;
}
   
// Driver code
public static void main(String[] args)
{
    int N = 3;
    int K = 40;
    PowerArray(N, K);
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python 3 implementation to find distinct
# powers of N that add up to K
  
from math import pow
  
# Function to return the highest power
# of N not exceeding K
def highestPower(n,k):
    i = 0
    a = pow(n, i)
  
    # Loop to find the highest power
    # less than K
    while (a <= k):
        i += 1
        a = pow(n, i)
    return i - 1
  
# Initializing the PowerArray
# with all 0's.
b = [0 for i in range(50)]
  
# Function to print
# the distinct powers of N
# that add upto K
def PowerArray(n, k):
    while (k):
        # Getting the highest
        # power of n before k
        t = highestPower(n, k)
  
        # To check if the power
        # is being used twice or not
        if (b[t]):
            # Print -1 if power
            # is being used twice
            print(-1)
            return 0
  
        else:
            # If the power is not visited,
            # then mark the power as visited
            b[t] = 1
  
        # Decrementing the value of K
        k -= pow(n, t)
  
    # Printing the powers of N
    # that sum up to K
    for i in range(50):
        if (b[i]):
            print(i,end = ', ')
  
# Driver code
if __name__ == '__main__':
    N = 3
    K = 40
    PowerArray(N, K)
      
# This code is contributed by Surendra_Gangwar


C#
// C# implementation to find distinct
// powers of N that add up to K
  
using System;
  
public class GFG{
  
// Function to return the highest power
// of N not exceeding K
static int highestPower(int n, int k)
{
    int i = 0;
    int a = (int) Math.Pow(n, i);
  
    // Loop to find the highest power
    // less than K
    while (a <= k) {
        i += 1;
        a = (int) Math.Pow(n, i);
    }
    return i - 1;
}
  
// Initializing the PowerArray
// with all 0's.
static int []b = new int[50];
  
// Function to print
// the distinct powers of N
// that add upto K
static int PowerArray(int n, int k)
{
    while (k > 0) {
  
        // Getting the highest
        // power of n before k
        int t = highestPower(n, k);
  
        // To check if the power
        // is being used twice or not
        if (b[t] > 0) {
  
            // Print -1 if power
            // is being used twice
            Console.Write(-1);
            return 0;
        }
  
        else
            // If the power is not visited,
            // then mark the power as visited
            b[t] = 1;
  
        // Decrementing the value of K
        k -= (int)Math.Pow(n, t);
    }
  
    // Printing the powers of N
    // that sum up to K
    for (int i = 0; i < 50; i++) {
        if (b[i] > 0) {
            Console.Write(i+ ", ");
        }
    }
    return 0;
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 3;
    int K = 40;
    PowerArray(N, K);
}
}
  
// This code is contributed by 29AjayKumar


C++
// C++ implementation to find out
// the powers of N that add upto K
  
#include 
using namespace std;
  
// Initializing the PowerArray
// with all 0's
int b[50] = { 0 };
  
// Function to find the powers of N
// that add up to K
int PowerArray(int n, int k)
{
  
    // Initializing the counter
    int count = 0;
  
    // Executing the while
    // loop until K is
    // greater than 0
    while (k) {
        if (k % n == 0) {
            k /= n;
            count++;
        }
  
        // If K % N == 1,
        // then the power array
        // is incremented by 1
        else if (k % n == 1) {
            k -= 1;
            b[count]++;
  
            // Checking if any power is
            // occurred more than once
            if (b[count] > 1) {
                cout << -1;
                return 0;
            }
        }
  
        // For any other value, the sum of
        // powers cannot be added up to K
        else {
            cout << -1;
            return 0;
        }
    }
  
    // Printing the powers of N
    // that sum up to K
    for (int i = 0; i < 50; i++) {
        if (b[i]) {
            cout << i << ", ";
        }
    }
}
  
// Driver code
int main()
{
    int N = 3;
    int K = 40;
    PowerArray(N, K);
    return 0;
}


Java
// Java implementation to find out
// the powers of N that add upto K
class GFG{
  
// Initializing the PowerArray
// with all 0's
static int b[] = new int[50];
  
// Function to find the powers of N
// that add up to K
static int PowerArray(int n, int k)
{
  
    // Initializing the counter
    int count = 0;
  
    // Executing the while
    // loop until K is
    // greater than 0
    while (k > 0) 
    {
        if (k % n == 0) 
        {
            k /= n;
            count++;
        }
  
        // If K % N == 1,
        // then the power array
        // is incremented by 1
        else if (k % n == 1)
        {
            k -= 1;
            b[count]++;
  
            // Checking if any power is
            // occurred more than once
            if (b[count] > 1)
            {
                System.out.print(-1);
                return 0;
            }
        }
  
        // For any other value, the sum of
        // powers cannot be added up to K
        else
        {
            System.out.print(-1);
            return 0;
        }
    }
  
    // Printing the powers of N
    // that sum up to K
    for(int i = 0; i < 50; i++)
    {
       if (b[i] != 0)
       {
           System.out.print(i + ", ");
       }
    }
    return Integer.MIN_VALUE;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 3;
    int K = 40;
    PowerArray(N, K);
}
}
  
// This code is contributed by Rajput-Ji


C#
// C# implementation to find out
// the powers of N that add upto K
using System;
  
class GFG{
  
// Initializing the PowerArray
// with all 0's
static int []b = new int[50];
  
// Function to find the powers of N
// that add up to K
static int PowerArray(int n, int k)
{
  
    // Initializing the counter
    int count = 0;
  
    // Executing the while loop 
    // until K is greater than 0
    while (k > 0) 
    {
        if (k % n == 0) 
        {
            k /= n;
            count++;
        }
  
        // If K % N == 1, then 
        // the power array is 
        // incremented by 1
        else if (k % n == 1)
        {
            k -= 1;
            b[count]++;
  
            // Checking if any power is
            // occurred more than once
            if (b[count] > 1)
            {
                Console.Write(-1);
                return 0;
            }
        }
  
        // For any other value, the sum of
        // powers cannot be added up to K
        else
        {
            Console.Write(-1);
            return 0;
        }
    }
  
    // Printing the powers of N
    // that sum up to K
    for(int i = 0; i < 50; i++)
    {
       if (b[i] != 0)
       {
           Console.Write(i + ", ");
       }
    }
    return int.MinValue;
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 3;
    int K = 40;
      
    PowerArray(N, K);
}
}
  
// This code is contributed by Rohit_ranjan


输出:
0, 1, 2, 3,

时间复杂度: O((log N) 2 )

  • 找出功率所需的时间为Log(N)
  • 最重要的是,另一个Log(N)循环用于K。
  • 因此,整体时间复杂度为Log(N) 2

高效的方法:可以观察到的另一个结论是,要使K为只能使用一次的N的幂的和,则K%N必须为10 (由于N 0,所以为1)。因此,如果( K%N )的结果不是0或1,则可以得出不可能得到总和K的结论。

因此,通过使用上面的观察,可以按照以下步骤计算答案:

  1. 首先,将计数器初始化为0。
  2. 如果(K%N)= 0 ,则计数器增加1,并且K更新为K / N。
  3. 如果K%N = 1 ,则频率数组f [count]增加1,并且K更新为K – 1
  4. 如果在任何时候该f [count]大于1,则返回-1(因为相同的功率不能使用两次)。

下面是上述方法的实现:

C++

// C++ implementation to find out
// the powers of N that add upto K
  
#include 
using namespace std;
  
// Initializing the PowerArray
// with all 0's
int b[50] = { 0 };
  
// Function to find the powers of N
// that add up to K
int PowerArray(int n, int k)
{
  
    // Initializing the counter
    int count = 0;
  
    // Executing the while
    // loop until K is
    // greater than 0
    while (k) {
        if (k % n == 0) {
            k /= n;
            count++;
        }
  
        // If K % N == 1,
        // then the power array
        // is incremented by 1
        else if (k % n == 1) {
            k -= 1;
            b[count]++;
  
            // Checking if any power is
            // occurred more than once
            if (b[count] > 1) {
                cout << -1;
                return 0;
            }
        }
  
        // For any other value, the sum of
        // powers cannot be added up to K
        else {
            cout << -1;
            return 0;
        }
    }
  
    // Printing the powers of N
    // that sum up to K
    for (int i = 0; i < 50; i++) {
        if (b[i]) {
            cout << i << ", ";
        }
    }
}
  
// Driver code
int main()
{
    int N = 3;
    int K = 40;
    PowerArray(N, K);
    return 0;
}

Java

// Java implementation to find out
// the powers of N that add upto K
class GFG{
  
// Initializing the PowerArray
// with all 0's
static int b[] = new int[50];
  
// Function to find the powers of N
// that add up to K
static int PowerArray(int n, int k)
{
  
    // Initializing the counter
    int count = 0;
  
    // Executing the while
    // loop until K is
    // greater than 0
    while (k > 0) 
    {
        if (k % n == 0) 
        {
            k /= n;
            count++;
        }
  
        // If K % N == 1,
        // then the power array
        // is incremented by 1
        else if (k % n == 1)
        {
            k -= 1;
            b[count]++;
  
            // Checking if any power is
            // occurred more than once
            if (b[count] > 1)
            {
                System.out.print(-1);
                return 0;
            }
        }
  
        // For any other value, the sum of
        // powers cannot be added up to K
        else
        {
            System.out.print(-1);
            return 0;
        }
    }
  
    // Printing the powers of N
    // that sum up to K
    for(int i = 0; i < 50; i++)
    {
       if (b[i] != 0)
       {
           System.out.print(i + ", ");
       }
    }
    return Integer.MIN_VALUE;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 3;
    int K = 40;
    PowerArray(N, K);
}
}
  
// This code is contributed by Rajput-Ji

C#

// C# implementation to find out
// the powers of N that add upto K
using System;
  
class GFG{
  
// Initializing the PowerArray
// with all 0's
static int []b = new int[50];
  
// Function to find the powers of N
// that add up to K
static int PowerArray(int n, int k)
{
  
    // Initializing the counter
    int count = 0;
  
    // Executing the while loop 
    // until K is greater than 0
    while (k > 0) 
    {
        if (k % n == 0) 
        {
            k /= n;
            count++;
        }
  
        // If K % N == 1, then 
        // the power array is 
        // incremented by 1
        else if (k % n == 1)
        {
            k -= 1;
            b[count]++;
  
            // Checking if any power is
            // occurred more than once
            if (b[count] > 1)
            {
                Console.Write(-1);
                return 0;
            }
        }
  
        // For any other value, the sum of
        // powers cannot be added up to K
        else
        {
            Console.Write(-1);
            return 0;
        }
    }
  
    // Printing the powers of N
    // that sum up to K
    for(int i = 0; i < 50; i++)
    {
       if (b[i] != 0)
       {
           Console.Write(i + ", ");
       }
    }
    return int.MinValue;
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 3;
    int K = 40;
      
    PowerArray(N, K);
}
}
  
// This code is contributed by Rohit_ranjan
输出:
0, 1, 2, 3,

时间复杂度:由于没有每次都检查最高功率,因此该算法的运行时间为log(N)