zoukankan      html  css  js  c++  java
  • HDU 4301 Divide Chocolate (DP + 递推)

    Divide Chocolate

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


    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这么大的巧克力 ,要分成M种,问你有多少种分法。

        先定义dp[i][j][z],i表示第i列,j表示i列内分成j部分,z表示第i列的两块巧克力处于分开的状态还合并的。

    所以 , 如果添加第i+1列则有三种情况:

    1,  还是j部分,也就是不加部分 :dp[i][j][1]=dp[i-1][j][0]*2+dp[i-1][j][1] ;   dp[i][j][0]=dp[i-1][j][0];

    2,       加一部分,j+1:   dp[i][j][1]=dp[i-1][j-1][0]+dp[i-1][j-1][1];     dp[i][j][0]=dp[i-1][j-1][1]+dp[i-1][j-1][1]+2*dp[i-1][j-1][0];

    3,       加两部分,j+2:   dp[i][j][0]=dp[i-1][j-2][1]+dp[i-1][j-2][0];

    这里有个图可以帮助理解:http://blog.csdn.net/youngyangyang04/article/details/7764817

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=1010;
    const int mod=100000007;
    
    int dp[N][N+N][2];  //dp[i][j][z],i表示第i列,j表示i列内分成j部分,z表示第i列的两块巧克力处于分开的状态还合并的(0:分开,1:合并)
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n,k;
        memset(dp,0,sizeof(dp));
        dp[1][1][1]=1;dp[1][2][0]=1;
        for(int i=2;i<N;i++){
            for(int j=1;j<=i*2;j++){
                dp[i][j][1]=(2*dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j-1][0]+dp[i-1][j-1][1])%mod;
                dp[i][j][0]=(dp[i-1][j][0]+2*dp[i-1][j-1][1]+2*dp[i-1][j-1][0]+dp[i-1][j-2][0]+dp[i-1][j-2][1])%mod;
            }
        }
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&k);
            printf("%d
    ",(dp[n][k][1]+dp[n][k][0])%mod);
        }
        return 0;
    }
  • 相关阅读:
    算法刷题训练(2020.10.6)
    使用Gitee作为图片仓库,博客园引入图片【2020.10.3】
    阿里云DevOps助理工程师认证学习笔记:敏捷项目管理+需求分析【2020.10.2 10.3】
    算法刷题训练(2020.9.30)
    爬取百度贴吧数据(练习Python爬虫)
    算法刷题训练(2020.9.29)
    算法刷题训练(2020.9.28)
    软件测试:软件开发模型和软件测试模型
    计算机组成原理练习题(有助于理解概念)
    wamp环境下Wordpress的安装
  • 原文地址:https://www.cnblogs.com/jackge/p/3236032.html
Copyright © 2011-2022 走看看