Express as sum of power of natural numbers | Practice | GeeksforGeeks

Given two numbers n and x, find out the total number of ways n can be expressed as sum of xth power of unique natural numbers.As total number of ways can be very large ,so return the number of ways modulo 109 + 7. 

Example 1:
Input: n = 10, x = 2
Output: 1 
Explanation: 10 = 12 + 32, Hence total 1 possibility. 


Example 2:
Input: n = 100, x = 2
Output: 3
Explanation: 100 = 102 
62 + 82 and 12 + 32 + 42 + 52 + 72 
Hence total 3 possibilities. 


Your Task:  
You dont need to read input or print anything. Complete the function numOfWays() which takes n and x as input parameter and returns the total number of ways n can be expressed as sum of xth power of unique natural numbers.

Expected Time Complexity: O(n2logn)
Expected Auxiliary Space: O(n)

Constraints:
1 <= n <= 103
1 <= x <= 5
0
ksandeep27072 months ago
DP solution:-
class Solution{
    public:
    int numOfWays(int n, int x)
    {
         vector<long long int > v(n,0);
  
         for (int i =0; i <n; ++i)
         {
            v[i]=(long long int)pow(i+1,x);
         }
          int sum=n;
          long long int dp[n+1][sum+1];

        for(int i=0;i<=n;i++)
            dp[i][0]=1;
        for(int j=1;j<=sum;j++)
            dp[0][j]=0;

     for (int i =1; i <=n; ++i)
     {
       for (int j = 1; j <=sum; ++j)
       {
           dp[i][j]=dp[i-1][j]%(1000000007);
           if(j>=v[i-1])
             dp[i][j]=(dp[i][j]+dp[i-1][j-v[i-1]])%(1000000007);
       }
     }
     return dp[n][sum]%(1000000007);
    }
};
0
araj2920183 months ago
class Solution{   public:   long long int t[1001][1001];   long long int mod=1e9+7;   long long int solve(int n,int x,int i){       if(n==0){           return 1;       }       if(i>n){           return 0;       }       if(t[n][i]!=-1){           return t[n][i];       }       int pa=pow(i,x);       if(pa<=n){           return t[n][i]=(solve(n-pa,x,i+1)+solve(n,x,i+1))%mod;                  }       else{           return t[n][i]=0;                  }   }   int numOfWays(int n, int x)   {       memset(t,-1,sizeof(t));       return solve(n,x,1);          }};
+2
priyasingh073 months ago
class Solution{
    public:
    long long int dp[1001][1001];
    long long int p=1e9+7;
    long long int ans(int n, int x, int i)
    {
        if(n==0)
        return 1;
        if(i>n)
        return 0;
        if(dp[n][i]!=-1)
        return (dp[n][i]%p);
        long long int t=pow(i,x);
        if(t<=n)
        {
            dp[n][i]=ans(n-t, x, i+1)+ans(n, x, i+1);
            return dp[n][i]%p;
        }
        else
        {
            dp[n][i]=0;
            return dp[n][i]%p;
        }
    }
    long long int numOfWays(int n, int x)
    {
        // code here
        memset(dp, -1, sizeof(dp));
        return ans(n, x, 1);
    }
};
0
sangamchoudhary73 months ago
Given function code is wrongMod please correct it , that's why people shift toward LeetCode
To prevent overflow → I used long long it
class Solution{
    public:
    long long int dp[1001][1001];
    long long int find(int i,int sum,int limit,int x,int n){
        if(i > limit+1) return 0; // as we can't miss the ceil of limit
        
        else if(sum > n) return 0;
        
        else if(dp[i][sum] != -1) return dp[i][sum];
        
        else if(sum == n)   return 1;
        
        long long int withPowSum = find(i+1,sum + pow(i,x),limit,x,n);            
        
        long long int withoutPowSum = find(i+1,sum,limit,x,n);
        
        return dp[i][sum] = withPowSum + withoutPowSum;
    }
    
    long long int numOfWays(int n, int x){
        int limit = pow(n,1.0/x);
        memset(dp,-1,sizeof dp);
        return find(1,0,limit,x,n);
    }
};
0
vankshu593 months ago
Use long long to prevent overflow
TC: O(N  N^(1/x)  * log(x))
class Solution {
    vector<vector<long long>> dp;
  public:
    long long numOfWays(long long n, int x) {
        int num = pow(n, 1.0 / x);
        dp.resize(n + 1, vector<long long>(num + 1, -1));
        return util(n, x, num);
    }
    
    long long util(int n, int x, int curr) {
        if(n == 0) return 1;
        else if(curr == 0) return 0;
        long long &ans = dp[n][curr];
        if(ans != -1) return ans;
        long long power = pow(curr, x);
        return ans = (n - power >= 0 ? util(n - power, x, curr - 1): 0) + util(n, x, curr - 1);
    }
};
0
kh4bnl3wsyyoqfbgviezyy2frk58ob5nee8ixigi
This comment was deleted.
0
wallflower9 months ago
wallflower
Backtracking solution. Find all numbers from 1 to n powered to x. And then backtrack to find all possible solutions.
https://ide.geeksforgeeks.o...
+4
Bharat Gupta1 year ago
Bharat Gupta
    long long numOfWays(int n, int x)    {        // Here, dp[i] represents number of ways to represent i        long long dp[n+1];        memset(dp, 0, sizeof(dp));                // Always 1 way to represent 0 or 1        // as sum of powers of natural numbers        dp[0] = dp[1] = 1;                // For n == 10 and x == 2, we will only try till 3^2        // else, the sum will exceed n        int maxLimit = pow(n, 1.0/x);        for(int i = 2; i <= maxLimit; i++){            int curr = pow(i, x);                        // Same as "include" condition in recursive code            // and since, we are running a loop from i to maxLimit            // the "exclude" condition is also handled.            for(int j = n; j >= curr; j--){                dp[j] += dp[j-curr];            }        }                return dp[n];    }
+2
niffoxic1 year ago
niffoxic
Why its saying use Dp if the topic tags says Recursion ?!
+5
aadi1551 year ago
aadi155
return type should be  long int otherwise it will give wrong answer.@moderator please look into the issue.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.