Given two numbers **N** and **K**, the task is to represent **K ^{N}** as the sum of exactly

**N**numbers. Print

**NA**if no such numbers are possible.

**Examples:**

Input:N = 5, K = 2

Output:2 2 4 8 16

Explanation:

2 + 2 + 4 + 8 + 16 = 32 = 2^{5}

Input:N = 4, K = 3

Output:3 6 18 54

Explanation:

3 + 6 + 18 + 54 = 81 = 3^{4}

**Approach:** In order to obtain numbers such that their sum is a power of K, we can choose the numbers that follow the condition:

This will always give the sum as a power of K.

**For example:** This can be illustrated as:

Let N = 3 and K = 4. We need to represent 4^{3}(=64) as the sum of exactly 3 numbers According to the mentioned approach, The 3 numbers which can be chosen are (4^{1}) = 4 (4^{2}- 4^{1}) = 16 - 4 = 12 (4^{3}- 4^{2}) = 64 - 16 = 48 Adding the numbers = 4 + 12 + 48 = 64 which is clearly 4^{3}Therefore the required 3 numbers are 4, 12 and 48.

Below is the implementation of the above approach:

## C++

`// C++ program to represent K^N ` `// as the sum of exactly N numbers ` ` ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to print N numbers whose ` `// sum is a power of K ` `void` `print(ll n, ll k) ` `{ ` ` ` `// Printing K ^ 1 ` ` ` `cout << k << ` `" "` `; ` ` ` ` ` `// Loop to print the difference of ` ` ` `// powers from K ^ 2 ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `ll x = ` `pow` `(k, i) - ` `pow` `(k, i - 1); ` ` ` `cout << x << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll N = 3, K = 4; ` ` ` `print(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to represent K^N ` `// as the sum of exactly N numbers ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to print N numbers whose ` `// sum is a power of K ` `static` `void` `print(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Printing K ^ 1 ` ` ` `System.out.print(k+ ` `" "` `); ` ` ` ` ` `// Loop to print the difference of ` ` ` `// powers from K ^ 2 ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) { ` ` ` `int` `x = (` `int` `) (Math.pow(k, i) - Math.pow(k, i - ` `1` `)); ` ` ` `System.out.print(x+ ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `, K = ` `4` `; ` ` ` `print(N, K); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to represent K^N ` `# as the sum of exactly N numbers ` `from` `math ` `import` `pow` ` ` `# Function to print N numbers whose ` `# sum is a power of K ` `def` `printf(n, k): ` ` ` ` ` `# Printing K ^ 1 ` ` ` `print` `(` `int` `(k),end ` `=` `" "` `) ` ` ` ` ` `# Loop to print the difference of ` ` ` `# powers from K ^ 2 ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `, ` `1` `): ` ` ` `x ` `=` `pow` `(k, i) ` `-` `pow` `(k, i ` `-` `1` `) ` ` ` `print` `(` `int` `(x),end` `=` `" "` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `3` ` ` `K ` `=` `4` ` ` `printf(N, K) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to represent K^N ` `// as the sum of exactly N numbers ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to print N numbers whose ` `// sum is a power of K ` `static` `void` `print(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Printing K ^ 1 ` ` ` `Console.Write(k+ ` `" "` `); ` ` ` ` ` `// Loop to print the difference of ` ` ` `// powers from K ^ 2 ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `int` `x = (` `int` `) (Math.Pow(k, i) - Math.Pow(k, i - 1)); ` ` ` `Console.Write(x+ ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 3, K = 4; ` ` ` `print(N, K); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

4 12 48

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Represent N as sum of K even numbers
- Represent K as sum of N-bonacci numbers
- Represent N as sum of K odd numbers with repetitions allowed
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Represent the given number as the sum of two composite numbers
- Represent the fraction of two numbers in the string format
- Represent a number as sum of minimum possible psuedobinary numbers
- Minimum count of numbers required from given array to represent S
- Sort numbers based on count of letters required to represent them in words
- Represent n as the sum of exactly k powers of two | Set 2
- Represent a given set of points by the best possible straight line
- Count of integers up to N which represent a Binary number
- Count of matchsticks required to represent the given number
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Count of sticks required to represent the given string
- Maximum number of distinct positive integers that can be used to represent N
- Count of ways to represent N as sum of a prime number and twice of a square
- Number of distinct ways to represent a number as sum of K unique primes
- Minimum number of given powers of 2 required to represent a number
- Add two integers of different base and represent sum in smaller base of the two

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.