-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount even length .txt
60 lines (60 loc) · 1.33 KB
/
Count even length .txt
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
48
49
50
51
52
53
54
55
56
57
58
59
60
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));
StringBuffer sb=new StringBuffer();
int T=Integer.parseInt(br.readLine());
while(T-->0)
{
int n=Integer.parseInt(br.readLine());
int[][] dp=new int[n+1][2*n+3];
for(int[] x:dp)
{
Arrays.fill(x,-1);
}
sb.append(count(n,0,dp)+"\n");
}
System.out.print(sb);
}
public static int count(int n,int diff,int[][] dp)
{
if(diff==0 && n==0)
{
return 1;
}
if(Math.abs(diff)>n || n<0)
{
return 0;
}
if(diff<0)
{
if(dp[n][Math.abs(diff)+n]!=-1)
{
return dp[n][Math.abs(diff)+n];
}
}
else{
if(dp[n][diff]!=-1)
{
return dp[n][diff];
}
}
int ans=(2*(count(n-1,diff,dp)%1000000007))%1000000007;
ans+=count(n-1,diff+1,dp)%1000000007;
ans=ans%1000000007;
ans+=count(n-1,diff-1,dp)%1000000007;
ans=ans%1000000007;
if(diff<0)
{
dp[n][Math.abs(diff)+n]=ans;
}
else{
dp[n][diff]=ans;
}
return ans;
}
}