编程语言
首页 > 编程语言> > DP algorithm to solve problem 3014 on poj.org by C++

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;
      }

标签:cakes,algorithm,there,3014,poj,empty,plates,problem,dp
来源: https://blog.csdn.net/alex_yuqian/article/details/106978948