The Power Sum


Problem Statement :


Find the number of ways that a given integer, X , can be expressed as the sum of the  Nth powers of unique, natural numbers.

For example, if X =  13 and N =  2 , we have to find all combinations of unique squares adding up to 13. The only solution is 2^2 + 3^2.

Function Description

Complete the powerSum function in the editor below. It should return an integer that represents the number of possible combinations.

powerSum has the following parameter(s):

X: the integer to sum to
N: the integer power to raise numbers to


Input Format

The first line contains an integer X.
The second line contains an integer N.

Constraints

1  <=   X  <=  1000
2  <=   N   <=   10

Output Format

Output a single integer, the number of possible combinations caclulated.



Solution :



title-img


                            Solution in C :

In   C++  :





#include <iostream>
#include <vector>

using namespace std;

int power (int a, int n) {
    if(n == 0)
        return 1;
    // else
    if(n % 2 == 0) {
        int temp = power(a, n / 2);
        return temp * temp;
    }
    // else
    return a * power(a, n - 1);
}

int solve(int x, const vector<int> &powers, int index) {
    if(index == 0) {
        return (x == 1) ? 1 : 0;
    }
    // else
    if(x == powers[index])
        return 1 + solve(x, powers, index - 1);
    // else
    int res = 0;
    res += solve(x - powers[index], powers, index - 1);
    res += solve(x, powers, index - 1);
    return res;
}


int main() {
    int x, n;
    cin >> x >> n;
    
    int pow = 1;
    vector<int> powers;
    for(int a = 2; pow <= x; a++) {
        powers.push_back(pow);
        pow = power(a, n);
    }
    
    cout << solve(x, powers, powers.size() - 1) << endl;        
    return 0;
}







In     Java  :




import java.util.Scanner;


public class PowerSum {
	int count=0;
	int sum;
	int pow;
    public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int x=in.nextInt();
		int n=in.nextInt();
        PowerSum p=new PowerSum();
        p.sum=x;
        p.pow=n;
        int N=(int)Math.pow(x, (1.0/n));
        p.getcount(p.sum,N,true);
        p.getcount(p.sum,N,false);
        System.out.println(p.count);
        in.close();
	}
    void getcount(int sum1,int n,boolean lenyani){
    	
    	if(lenyani==true){
    		sum1=sum1-(int)Math.pow(n, pow);
    	
    	}
    	if(sum1<0) return;
    	if(sum1==0){
    		count++;
    		return;
    	}
    	if(n==1) return;
    	getcount(sum1,n-1,true);
    	getcount(sum1,n-1,false);
    }
 
}








In    C   :







#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int the_power_sum(int n, int m,int p){
    int tmp = pow(m,p);
    if(tmp == n) return 1;
    if(tmp > n) return 0;
 return the_power_sum(n,m+1,p) + the_power_sum(n-tmp, m+1,p);
}
int main() {
    int n,p;
    scanf("%d%d",&n,&p);
    printf("%d", the_power_sum(n,1,p));
 
    return 0;
}









In   Python3 :





import sys
X = int(input())
N = int(input())

def rec(X,N,start):
    count = 0 
    for i in range(start,X):
        ans = X-i**N
        if ans == 0:
            count += 1
        if ans> 0 :
            count += rec(ans,N,i+1)
        if ans<0:
            continue   
    return count
    
print(rec(X,N,1))
                        








View More Similar Problems

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →