MUTC 1 A Divide Chocolate 方案统计DP

Divide Chocolate

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1271    Accepted Submission(s): 604


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.
 

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
 

-------------------------

问,把n*2的巧克力分成k分,有几种不同方法。

第一反应是用f[i][j]表示前i排巧克力分成j份的方法数,可是这样难以写出状态转移方程。

正确的做法是用f[i][j][0]表示将前i排巧克力分成j份第i排为属于同一块的方法数,用f[i][j][1]表示将前i排巧克力分成j份第i排属于不同块的方法数。

则有状态转移方程如下

①  f[i+1][j][0]+=f[i][j][0]+f[i][j][1]*2; 

巧克力分成的份数不变,新增加一排一体的巧克力。若i排为整体,则i+1排巧克力可以直接并在i排上(f[i][j][0]);若i排为两份巧克力,则i+1排可连接到第一排或第二排,有两种方案(f[i][j][1]*2)。

② f[i+1][j+1][0]+=f[i][j][0]+f[i][j][1];

新增一排独立的巧克力。不管i排是独立(f[i][j][1])还是一体(f[i][j][0]),直接连接上一份1*2的巧克力。巧克力份数+1。

③ f[i+1][j][1]+=f[i][j][1];

将第i排两份独立的巧克力延伸出来,巧克力份数不变。

④ f[i+1][j+1][1]+=f[i][j][0]*2+f[i][j][1]*2;

在f[i][j][0]上添加一块1*1的新巧克力,延伸一块巧克力,有两种方案。f[i][j][1]同理。份数+1。

⑤f[i+1][j+2][1]+=f[i][j][0]+f[i][j][1];

直接添加两块1*1的巧克力,份数+2。

f[n][k][0]+f[n][k][1]即为答案。k是n的二倍,注意数组不能开小。

先预处理出f[][][]然后根据输入输出答案,否则会超时。

-------------------------

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int f[1111][2222][2];

const int mod=100000007;

int main()
{
    int T,n,k;
    memset(f,0,sizeof(f));
    f[1][1][0]=1;
    f[1][2][1]=1;
    for (int i=0; i<=1000; i++)
    {
        for (int j=0; j<=i*2; j++)
        {
            f[i+1][j][0]=(f[i+1][j][0]+f[i][j][0]+f[i][j][1]*2)%mod;
            f[i+1][j+1][0]=(f[i+1][j+1][0]+f[i][j][0]+f[i][j][1])%mod;
            f[i+1][j][1]=(f[i+1][j][1]+f[i][j][1])%mod;
            f[i+1][j+1][1]=(f[i+1][j+1][1]+f[i][j][0]*2+f[i][j][1]*2)%mod;
            f[i+1][j+2][1]=(f[i+1][j+2][1]+f[i][j][0]+f[i][j][1])%mod;
        }
    }
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&k);
        printf("%d\n",(f[n][k][0]+f[n][k][1])%mod);
    }
    return 0;
}







原文地址:https://www.cnblogs.com/cyendra/p/3226312.html