1051: Divide Chocolate
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 31
[ Submit][ Status][ Web Board]
Description
It is well known that Claire likes dessert very much, especially chocolate. But as a girl she also focuses on the intake of calories each day. To satisfy both of the two desires, Claire makes a decision that each chocolate should be divided into several parts, and each time she will enjoy only one part of the chocolate. Obviously clever Claire can easily accomplish the division, but she is curious about how many ways there are to divide the chocolate.
To simplify this problem, the chocolate can be seen as a rectangular contains n*2 grids (see above). And for a legal division plan, each part contains one or more grids that are connected. We say two grids are connected only if they share an edge with each other or they are both connected with a third grid that belongs to the same part. And please note, because of the amazing craft, each grid is different with others, so symmetrical division methods should be seen as different.
Input
First line of the input contains one integer indicates the number of test cases. For each case, there is a single line containing two integers n (1<=n<=1000) and k (1<=k<=2*n).n denotes the size of the chocolate and k denotes the number of parts Claire wants to divide it into.
Output
For each case please print the answer (the number of different ways to divide the chocolate) module 100000007 in a single line.
Sample Input
Sample Output
HINT
Source
具有思考价值,分类难想到,首先将其表示为dp[i][j],i表示为前i列,分成j个部分的方法数,虽然这容易想到,但方程式仅由此很难推断出来,观察到是一个2*n的因此可以将其再进行限制,对于上下两块,要么连在一起,要么不连,于是可想到dp[i][j][k],k表示第i列的两块是否连在一起,有了限制之后,方程式就容易写出来了。另此情形下,若高不为2,为3,或者更大,以此类推即可,例如若是3*n,则分为3个不连在一起,上面2个连在一起,下面两个连在一起,最上与最下连在一起,三个连在一起的情况,此时k为5.
#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define pi acos(-1)
#define mod 100000007
int dp[1001][2001][2];
int main() {
int n, k;
dp[1][1][0] = 1;
dp[1][2][1] = 1;
for (int i = 2; i <= 1000; ++i) {
int l = i << 1;
dp[i][1][0] = 1;
for (int j = 2; j <= l; ++j) {
dp[i][j][0] += dp[i - 1][j][0];
dp[i][j][0] %= mod;
dp[i][j][0] += dp[i - 1][j - 1][0];
dp[i][j][0] %= mod;
dp[i][j][0] += 2 * dp[i - 1][j][1];
dp[i][j][0] %= mod;
dp[i][j][0] += dp[i - 1][j - 1][1];
dp[i][j][0] %= mod;
dp[i][j][1] += 2 * dp[i - 1][j - 1][0];
dp[i][j][1] %= mod;
dp[i][j][1] += dp[i - 1][j][1];
dp[i][j][1] %= mod;
dp[i][j][1] += 2*dp[i - 1][j - 1][1];
dp[i][j][1] %= mod;
dp[i][j][1] += dp[i - 1][j - 2][0];
dp[i][j][1] %= mod;
dp[i][j][1] += dp[i - 1][j - 2][1];
dp[i][j][1] %= mod;
}
}
int T;
cin >> T;
while (T--) {
scanf("%d%d", &n, &k);
printf("%d\n", (dp[n][k][0] + dp[n][k][1])%mod);
}
return 0;
}