(两种方法)POJ 2279 -- 线性dp || 杨氏矩阵+勾长公式

rt:

Mr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behind it and the left ends of the rows aligned. For instance, 12 students could be arranged in rows (from back to front) of 5, 3, 3 and 1 students.

   X X X X X

   X X X

   X X X

   X 

In addition, Mr. Young wants the students in each row arranged so that heights decrease from left to right. Also, student heights should decrease from the back to the front. Thinking about it, Mr. Young sees that for the 12-student example, there are at least two ways to arrange the students (with 1 as the tallest etc.):

   1  2  3  4  5     1  5  8 11 12

   6  7  8           2  6  9

   9 10 11           3  7 10

   12                4 

Mr. Young wonders how many different arrangements of the students there might be for a given arrangement of rows. He tries counting by hand starting with rows of 3, 2 and 1 and counts 16 arrangements:

   123 123 124 124 125 125 126 126 134 134 135 135 136 136 145 146

   45  46  35  36  34  36  34  35  25  26  24  26  24  25  26  25

   6   5   6   5   6   4   5   4   6   5   6   4   5   4   3   3 

Mr. Young sees that counting by hand is not going to be very effective for any reasonable number of students so he asks you to help out by writing a computer program to determine the number of different arrangements of students for a given set of rows.

dp:

状态:dp[i][j][k][m][n] : 第1行站i 个人,第二行站j个人,,,第五行站n个人的方案数
转移方程:在这里插入图片描述

于是就有代码:

#pragma G++ optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i = (a); i <(b); ++i)
#define _rof(i, a, b) for(int i = (a); i >(b); --i)
#define maxn 32
#define maxm 109
#define oo INT_MAX 
#define ll long long
#define met(a,b) memset((a),(b), sizeof(a))
#define db double
#define eps 1e-8
#define debug
#define letout(x) cout << x << endl 
using namespace std;
int r, a[maxn], dp[maxn][maxn][maxn][maxn][maxn], sum;
int main() {
 ios::sync_with_stdio(0);
 while (cin >> r && r) {
  _rep(i, 1, r) {
   cin >> a[i];
  }
  dp[0][0][0][0][0] = 1;
  _rep(i, 0, a[1]) {
   _rep(j, 0, a[2]) {
    _rep(k, 0, a[3]) {
     _rep(m, 0, a[4]) {
      _rep(n, 0, a[5]) {
       int num = dp[i][j][k][m][n];
       if (i < a[1])dp[i + 1][j][k][m][n] += num;
       if (j < a[2] && j < i)dp[i][j + 1][k][m][n] += num;
       if (k < a[3] && k < j)dp[i][j][k + 1][m][n] += num;
       if (m < a[4] && m < k)dp[i][j][k][m + 1][n] += num;
       if (n < a[5] && n < m)dp[i][j][k][m][n + 1] += num;
      }
     }
    }
   }
  }
  cout << dp[a[1]][a[2]][a[3]][a[4]][a[5]] << endl;
  memset(dp, 0, sizeof(dp));
 }
}

然而悲催的 MLE:

Do you want to solve it in dp anyway?

法2:

杨氏矩阵和勾长公式:
more details

#pragma G++ optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i = (a); i <(b); ++i)
#define _rof(i, a, b) for(int i = (a); i >(b); --i)
#define maxn 159
#define maxm 109
#define oo INT_MAX 
#define ll long long
#define met(a,b) memset((a),(b), sizeof(a))
#define db double
#define eps 1e-8
#define met(a, b) memset(a, b, sizeof(a))
#define debug
#define letout(x) cout << x << endl 
using namespace std;
inline ll
int r, a[maxn], sum[maxn];
int main() {
 ios::sync_with_stdio(0);
 while (cin >> r && r) {
  int all = 0;
  met(a, 0);
  met(sum, 0);
  _rep(i, 1, r) {
   cin >> a[i];
   all += a[i];
  }
  int cnt = 0;
  _rep(i, 1, r) {//枚举层
   _rep(j, 1, a[i]) {
    cnt++;
    _rep(k, i, r) {
     if (a[k] >= j)sum[cnt]++;
     else break;
    }
    sum[cnt] += a[i] - j;
   }
  }
  ll x = 1, y = 1;
  _rep(i, 1, all) {
   x *= i, y *= sum[i];
   ll tmp = __gcd(x, y);
   x /= tmp, y /= tmp;
  }
  cout << x / y << endl;
 }
}

and this got an AC eventually:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值