hdu 4031 Divide Chocolate

Dynamic Programming again!

There are a lot of state, so, the main point is how to classify them clearly.

dp[i][j][0]: as the number of different methods to divide 2 * i chocolate grid into j part and last two grid in i-th column are in the same connected component.
dp[i][j][1]: as the number of different methods to divide 2 * i chocolate grid into j part and last two grid in i-th column are in different  connected component.

Then we classify state into two situations and each has some sub-situations.

And coding.


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <queue>
#include <stack>
using namespace std;
#define MS(c, a) memset(c, a, sizeof c)
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
#define MAXN (2020)
#define MOD (100000007)
typedef long long LL;

LL dp[MAXN][MAXN][3];
int n, k;

int main()
{
	dp[1][1][0] = dp[1][2][1] = dp[1][1][2] = dp[1][2][2] = 1;
	Rep(i, 2, 1010) Rep(j, 1, i * 2 + 1)
	{
		int k = i - 1;
		dp[i][j][0] = (dp[k][j][0] + dp[k][j - 1][2] + 2 * dp[k][j][1]) % MOD;
		dp[i][j][1] = (2 * dp[k][j - 1][2] + dp[k][j][1]) % MOD;
		if (j > 2) dp[i][j][1] = (dp[i][j][1] + dp[k][j - 2][2]) % MOD;
		dp[i][j][2] = (dp[i][j][0] + dp[i][j][1]) % MOD;
	}
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &k);
		printf("%lld\n", dp[n][k][2]);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值