-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpress as sum of power of natural numbers
47 lines (42 loc) · 1.22 KB
/
Express as sum of power of natural numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while(T-->0)
{
String[] input = new String[2];
input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int x = Integer.parseInt(input[1]);
Solution ob = new Solution();
System.out.println(ob.numOfWays(n, x));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution
{
static int mod = 1000000007;
static int help(int n,int x,int num,int dp[][]){
if(n==0)return 1;
if(num>n || n<0)return 0;
if(dp[n][num]!=-1)return dp[n][num];
int temp = (int)Math.pow(num,x);
return dp[n][num]=(help(n-temp,x,num+1,dp)+help(n,x,num+1,dp))%mod;
}
static int numOfWays(int n, int x)
{
// code here
int dp[][] = new int[n+1][n+1];
for(int temp[]:dp)Arrays.fill(temp,-1);
return help(n,x,1,dp);
}
}