saymyname's blog

By saymyname, 10 years ago, In English

Hi, I solved this problem using recursive dp. problem link recursive solution link

How to convert this recursive solution into iterative solution?

Thanks in Advance

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it
#include <bits/stdc++.h> 
using namespace std; 
#define ll long long int 
#define M 1e9+7;
int count(int n, int k) 
{
	int dp[n + 1][k + 1][2]; 
	memset(dp, 0, sizeof(dp)); 

	dp[1][0][0] = 1; 
	dp[1][0][1] = 1; 

	for (int i = 2; i <= n; i++) { 
		for (int j = 0; j <=k; j++) { 
			dp[i][j][0] = ((dp[i - 1][j][0]%M) + (dp[i - 1][j][1]%M))%M; 
			dp[i][j][1] = (dp[i - 1][j][0])%M; 
			if (j - 1 >= 0)
				dp[i][j][1] += (dp[i - 1][j - 1][1])%M; 
		} 
	} 

	return ((dp[n][k][0]%M) + (dp[n][k][1]%M))%M; 
} 
int main() 
{ 
    ll n,m,k,i,j,x,z,t;
    cin>>t;
    while(t--)
    {
        cin>>j>>k;
        cout<<count(j,k)<<endl;
    }
	return 0; 
}