Divide Chocolate
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1536 Accepted Submission(s): 720
Problem 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.

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
2 2 1 5 2
Sample Output
1 45
Author
BUPT
Source
Recommend
zhuyuanchen520
这道题的内存好卡啊,要是三维DP开2000 * 2000 * 2有可能MLE。
优化内存的话滚动数组可以。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 2000 + 5;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x3f3f3f3f;
const int IMIN = 0x80000000;
const double E = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 100000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
// #pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
int dp[MAXN][MAXN][2];
int main()
{
int t;
cin >> t;
clr(dp, 0);
dp[1][1][0] = dp[1][2][1] = 1;
FOR(i , 2 , 1100)FORR(j , 1 , 2000)
{
if(j>2*i)
break;
if(j == 1)
{
dp[i][1][1] = 0;
dp[i][1][0] = 1;
}
else
{
dp[i][j][0] = (dp[i - 1][j][1] * 2 + dp[i - 1][j][0] + dp[i -1][j - 1][1] + dp[i - 1][j - 1][0]) % mod;
dp[i][j][1] = (dp[i - 1][j][1] + dp[i - 1][j - 1][1] * 2 + dp[i - 1][j - 1][0] * 2 + dp[i - 1][j - 2][0] + dp[i - 1][j - 2][1]) % mod;
}
}
while(t--)
{
int n , m;
scanf("%d%d" , &n , &m);
printf("%d\n" , (dp[n][m][0] + dp[n][m][1]) % mod);
}
return 0;
}