Description

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.

Input

The
input for each problem instance will consist of two lines. The first
line gives the number of rows, k, as a decimal integer. The second line
contains the lengths of the rows from back to front (n1, n2,..., nk) as
decimal integers separated by a single space. The problem set ends with a
line with a row count of 0. There will never be more than 5 rows and
the total number of students, N, (sum of the row lengths) will be at
most 30.

Output

The
output for each problem instance shall be the number of arrangements of
the N students into the given rows so that the heights decrease along
each row from left to right and along each column from back to front as a
decimal integer. (Assume all heights are distinct.) The result of each
problem instance should be on a separate line. The input data will be
chosen so that the result will always fit in an unsigned 32 bit integer.

Sample Input

1
30
5
1 1 1 1 1
3
3 2 1
4
5 3 3 1
5
6 5 4 3 2
2
15 15
0

Sample Output

1
1
16
4158
141892608
9694845

Solution:

本题是lyd神犇的进阶指南上的DP题,状态转移方程看懂了,但是看似简单去实现有点复杂啊,于是取搜罗本题的勾长公式的解法。

杨表由有限的方格组成。

对于一个正整数,给定一个整数分拆λ(10=1+4+5),则对应一个杨表(注意这是一个递降的过程,也就是说下面一行的方格数要大于等于上一行的方格数)。

 

一个(1,4,5)分拆表示的杨表

杨表与整数分拆λ一一对应。

给定一个杨表,一共有n个方格。那么把1到n这n个数字填到这个杨表中,使得每行从左到右都是递增的,每列从下到上也是递增的。如图

 

一个杨表的表示

【勾长】对于杨表中的一个方格v,其勾长hook(v)等于同行右边的方格数加上同列上面的方格数,再加上1(也就是他自己)。

【勾长公式】用表示杨表个数,则

 

对于分拆10 = 5 + 4 + 1 的应的杨表. 因此共有

种方法。

公式题。求出同行右边的方格数+同列上面的方格数+1。唯一注意的地方是最后除的时候要防止溢出。

转载:

这个题嘛,标准做法是线性DP。

f[a1,a2,a3,a4,a5]表示每排从左起占了a1,a2,a3,a4,a5个人的方案数,f[0,0,0,0,0]=1。

转移方程为:当a1<N1,f[a1+1,a2,a3,a4,a5]+=f[a1,a2,a3,a4,a5],其余同理。

上面是lyd讲的,本蒟蒻觉得有点错,但说不清哪儿有问题(也可能本身没问题),路过的dalao帮看一看,谢告知。

那么简单做法就是:先去了解一下杨氏矩形和勾长公式,然后直接用公式做。我这种蒟蒻就选了这种方法……

杨氏矩阵定义(需满足的条件/特征):

(1)若格子(i,j),则该格子的右边和上边一定没有元素;

(2)若格子(i,j)有元素data[i][j],则该格子右边和上边相邻的格子要么没有元素,要么有比data[i][j]大的元素。

显然有同已写元素组成的杨氏矩阵不唯一,1~n组成杨氏矩阵的个数可以写出:

F[1]=1,F[2]=2,F[n]=F[n-1]+(n-1)*F[n-2] (n>2)。

钩子长度的定义:该格子右边的格子数和它上边的格子数之和;

钩子公式:对于给定形状,不同的杨氏矩阵的个数为(n!/(每个格子的钩子长度加1的积))。

知道了这些再做这个题就很方便了……

代码:

#include<bits/stdc++.h>
#define ll long long
#define il inline
using namespace std;
ll n,cnt,x,y,tmp,num[],sum[];
il ll gcd(int a,int b){return b?gcd(b,a%b):a;}
int main()
{
while(scanf("%lld",&n)&&n){
memset(sum,,sizeof(sum));
cnt=,x=,y=;
for(int i=;i<=n;i++)scanf("%lld",&num[i]);
for(int i=;i<=n;i++)
for(int j=;j<=num[i];j++){
cnt++;
for(int k=i+;k<=n;k++){
if(num[k]>=j)sum[cnt]++;
else break;
}
sum[cnt]+=num[i]-j+;
}
for(int i=;i<=cnt;i++){
x*=i;y*=sum[i];
tmp=gcd(x,y);
x/=tmp;y/=tmp;
}
printf("%lld\n",x/y);
}
return ;
}

poj2279——Mr. Young's Picture Permutations的更多相关文章

  1. POJ2279 Mr Young's Picture Permutations

    POJ2279 Mr Young's Picture Permutations 描述: 有N个学生合影,站成左对齐的k排,每行分别有N1,N2…NK个人,第一排站最后,第k排站之前.学生身高依次是1… ...

  2. 【题解】POJ2279 Mr.Young′s Picture Permutations dp

    [题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...

  3. poj2279 Mr. Young's Picture Permutations[勾长公式 or 线性DP]

    若干人左对齐站成最多5行,给定每行站多少个,列数从第一排开始往后递减.要求身高从每排从左到右递增(我将题意篡改了便于理解233),每列从前向后递增.每个人身高为1...n(n<=30)中的一个数 ...

  4. bzoj 2483: Pku2279 Mr. Young's Picture Permutations -- 钩子公式

    2483: Pku2279 Mr. Young's Picture Permutations Time Limit: 1 Sec  Memory Limit: 128 MB Description   ...

  5. 轮廓线DP:poj 2279 Mr. Young's Picture Permutations

    poj 2279 Mr. Young's Picture Permutations \(solution:\) 首先摘取一些关键词:(每行不超过它后面的行)(每排学生安排高度从左到右减少)(学生的高度 ...

  6. Mr. Young's Picture Permutations

    Mr. Young's Picture Permutations 给出一个有k列的网格图,以及每列图形的高度\(n_i\),下端对齐,保证高度递减,设有n个网格,询问向其中填1~n保证每行每列单调递增 ...

  7. 【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations

    Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...

  8. [POJ 2279] Mr. Young's Picture Permutations

    [题目链接] http://poj.org/problem?id=2279 [算法] 杨氏矩阵与勾长公式 [代码] #include <algorithm> #include <bi ...

  9. POJ P2279 Mr. Young's Picture Permutations 题解

    每日一题 day14 打卡 Analysis 五维dpf[a1,a2,a3,a4,a5]表示各排从左端起分别占了a1,a2,a3,a4,a5个人时合影方案数量然后我们枚举a1,a2,a3,a4,a5从 ...

随机推荐

  1. Learning-Python【28】:基于TCP协议通信的套接字

    什么是 Socket Socket 是应用层与 TCP/IP 协议通信的中间软件抽象层,它是一组接口.在设计模式中,Socket 其实就是一个门面模式,它把复杂的 TCP/IP 协议族隐藏在 Sock ...

  2. Js — CommonUtil

    一些js脚本的公用方法: 1:字符串根据给定的每行长度换行 2:比较两个时间的大小3:计算两个日期间相差的天数 1.字符串根据给定的每行长度换行 /** *words:原始字符串 *avg:每行字数 ...

  3. _quest_mod

    该功能实现对任务的优化,设定接受任务的条件,比如VIP等级几或者军衔多少持有何种物品才可以接受任务,同时可以配置任务的随机奖励及几率,以上修改都会在任务文本中体现.还支持任务传送功能,接完任务后,可和 ...

  4. java笔记 -- java运算

    运算符: 算术运算符: 加减乘除求余 + , - , * , / , % 当参与/运算的两个操作数都是整数时, 表示整数除法, 否则表示浮点除法. 例: 15 / 2 = 7; 15 % 2 = 1; ...

  5. Hive数据导入导出

    Hive三种不同的数据导出的方式 (1)  导出到本地文件系统 insert overwrite local directory '/home/anjianbing/soft/export_data/ ...

  6. MATLAB绘图功能(1) 二维高层绘图操作

    文末源代码 部分源代码 %% 基本绘图操作 x=:*pi; y=sin(x); plot(x,y); % 第二个参数为矩阵 y1=sin(x); y2=cos(x); y3=0.002*exp(x); ...

  7. 三款免费实用的文件夹同步/备份软件推荐 (SyncToy/FreeFileSync/Compare Advance)

    三款免费实用的本地文件夹同步/备份软件推荐 (SyncToy/FreeFileSync/Compare Advance) Microsoft SyncToy SyncToy 是由 微软 推出的一款免费 ...

  8. mysql查看配置生效

    mysql> show status like 'Threads%';+-------------------+-------+| Variable_name     | Value |+--- ...

  9. js运行机制

    情况一 script标签里面的运行顺序是同步的 遇到settimeout的时候就会变异步,最后执行 执行顺序为1342 情况二 只输出a 情况三 输出4444 异步队列插入的时间和执行时间 for循环 ...

  10. Android : 跟我学Binder --- (6) JAVA实现

    目录: Android : 跟我学Binder --- (1) 什么是Binder IPC?为何要使用Binder机制? Android : 跟我学Binder --- (2) AIDL分析及手动实现 ...