DP algorithm to solve problem 3014 on poj.org by C++

  1. For this problem, there are 3 ways to solve, two DP algorithms to solve and one mathematical way to solve.

  2. Question meaning:Put m pieces of cakes into n plates, a plate can have 0 or multiple pieces of cakes, how many ways are there?
    (for more specific explanations for the question, please search problem 3014 in poj.org)

    1. Common DP algorithm:
      (1) If the question is the number of ways to put m plates into n pieces of cakes, we use dp[m][n] to express.
      (2) For normal dp[i][j], means the number of ways to put m pieces of cakes into n plates, after we solve all dp[i][j], we can get dp[m][m].
      (3) We need to think how can we get dp[i][j], which the question is being changed to a classification problem, and the core to solve it is the
      state transition equation.

    2. Method 1: DP algorithm
      (1) Firstly, we need to compare the number of cakes with the number of plates, which we can get 3 cases:
      plates > cakes, plates = cakes, plates < cakes.
      For each case, we need to condider whether there are empty plates or not.
      (2) Case 1: plates > cakes
      For this case, there must have empty plates, and even if you take one plates away it does not matter, which can expressed as
      dp[i-1][j]. (remenber, i repsents plates and j represents cakes)
      Case 2: plates = cakes
      There are two sub-cases to consider.
      Firstly, there are no empty plates, which means each plate has exactly one piece of cake. And this only has 1 case.
      Secondly, there are empty plates. Same as case 1, it’s OK for take away at least one plates since there are empty plates,
      which can be expresses as dp[i-1][j].
      In total, there are 1 + dp[i-1][j] cases for case 2.
      Case 3: plates < cakes
      There are two sub-cases.
      Firstly, there are no empty plates. Same as before, it can be expressed as dp[i-1][j] since at least take away one plate
      does not matter.
      Secondly, there are empty plates. We can think as for each plate put 1 cake from j cakes, specificially, at i plate, there
      are j-i cakes left, since i cakes are consumed, which we can expressed as dp[i][j-i].

    3. Original problem: http://poj.org/problem?id=3014

    4. Code:
      #include
      using namespace std;
      int dp[4503][4503];
      int main()
      {
      int m,n,i,j;
      scanf("%d%d",&m,&n);
      dp[1][1]=1;
      dp[1][0]=0;
      dp[0][1]=0;
      for(i=1;i<=m;i++)
      {
      for(j=1;j<=n;j++)
      {
      if(i>j)
      dp[i][j]=dp[i-1][j];
      if(i==j)
      dp[i][j]=dp[i-1][j]+1;
      if(i<j)
      dp[i][j]=dp[i-1][j]+dp[i][j-i];
      if(dp[i][j]>=1000000007)
      dp[i][j]-=1000000007;
      }
      }
      printf("%lld\n",dp[m][n]);
      return 0;
      }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值