Hi, I have written a bottom-up approach (Top-down approach was discussed by Tushar) To this problem.
I don’t know why my solution gets wrong answer please help!
Problem Link : https://hack.codingblocks.com/contests/c/204/814
My code :
#include<bits/stdc++.h>
using namespace std;
int dp[101][101][2];
const int INF = 1e6+3;
int main()
{
int t;cin >> t;
for(int i=0;i<=100;++i){
for(int j=0;j<2;++j){
dp[0][i][j]=1;
}
}
for(int i=1;i<=100;++i){
for(int j=0;j<=100;++j){
if(j>0) dp[i][j][1] = dp[i-1][j-1][1]%INF;
dp[i][j][1] += dp[i-1][j][0]%INF;
dp[i][j][0] = dp[i-1][j][0]%INF;
dp[i][j][0] += dp[i-1][j][1]%INF;
}
}
for(int i=0;i<t;++i){
int n,k;cin >> n >> k;
cout << dp[n][k][1]+dp[n][k][0] << endl;
}
}