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 题意:给出行数k,以及每行数字的个数n[i],问一共有多少种排列方法使元素从左到右从上到下依次递减。(即构成一个杨氏矩阵)。
分析:勾长公式。暴力+公式;

杨氏矩阵(杨表)(面试会问到)

杨表由有限的方格组成。

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

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

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

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

一个杨表的表示

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

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

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

种方法。

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

 LL ans = ;
for(int i = ; i <= tot; i++)
{
factor[i] = i;
for(int j = ; j <= tot; j++)
{
int tmp = gcd(factor[i], young[j]);
factor[i] /= tmp;
young[j] /= tmp;
}
}
//经过上述处理后young[i]均变为1,至于原因,分子除分母是整数,结果分母一定会变为1。
for(int i = ; i <= tot; i++)
{
ans *= factor[i];
}

代码:

 /*
Problem: poj_2279
tags: 杨氏矩阵+勾长公式
*/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define mems(x, t) memset(x, t, sizeof(x));
using namespace std;
typedef long long LL;
const int maxn = ;
const int INF = 0x3f3f3f3f; int k, tot, n[maxn];
int table[maxn][maxn], young[], factor[];
LL gcd(LL a, LL b)
{
return b == ? a : gcd(b, a%b);
}
int main()
{
while(~scanf("%d", &k) && k)
{
mems(table, );
tot = ;
for(int i = ; i <= k; i++)
{
scanf("%d", &n[i]);
tot += n[i];
for(int j = ; j <= n[i]; j++)
{
table[i][j] = ;
}
}
mems(young, );
int cnt = ;
for(int i = ; i <= k; i++)
{
for(int j = ; j <= n[i]; j++)
{
if(table[i][j] == )
{
young[cnt]++;
for(int l = i+; l <= k; l++)
{
if(!table[l][j]) break;
young[cnt]++;
}
young[cnt] += n[i]-j;
cnt++;
}
}
}
LL ans = ;
for(int i = ; i <= tot; i++)
{
factor[i] = i;
for(int j = ; j <= tot; j++)
{
int tmp = gcd(factor[i], young[j]);
factor[i] /= tmp;
young[j] /= tmp;
}
}
for(int i = ; i <= tot; i++)
{
ans *= factor[i];
}
printf("%lld\n", ans);
}
return ;
}

【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations的更多相关文章

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

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

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

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

  3. 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从 ...

  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. POJ2279 Mr Young's Picture Permutations

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

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

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

  7. Mr. Young's Picture Permutations

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

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

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

  9. poj2279——Mr. Young's Picture Permutations

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

随机推荐

  1. Java缓存学习之二:浏览器缓存机制

    浏览器端的九种缓存机制介绍 浏览器缓存是浏览器端保存数据用于快速读取或避免重复资源请求的优化机制,有效的缓存使用可以避免重复的网络请求和浏览器快速地读取本地数据,整体上加速网页展示给用户.浏览器端缓存 ...

  2. crontab 获取本机ip

    写了个shell获取ip的函数,如下 function GetLocalIP() { ifconfig | grep 'inet '| grep -v '127.0.0.1' | cut -d: -f ...

  3. HDU 5512 Pagodas (2015沈阳现场赛,找规律+gcd)

    Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  5. Elasticsearch简单介绍

    如何对站内的数据进行检索? ElasticSearch是比较著名的一个分布式检索解决方案.传统的数据库例如mysql,oracle等,对一个关键词进行检索通常都是采用like的匹配,对性能或者数据量的 ...

  6. 去除Linq to Sql 查询结果中的空格

    原来的写法: Dim db = From city In DataContext1.AddressCity Where city.ProvinceID = dgvAddress(2, e.RowInd ...

  7. ubuntu为IDE(Eclipse WebStorm)添加桌面快捷方式

    在ubuntu15.10环境配置webstorm和eclipse的时候会下载官网上编译好的包, bin目录下面会有一个.sh文件(linux版本), 那么一般情况下,执行 ./sh就会启动IDE, 但 ...

  8. Linux在向磁盘新建文件的时候在系统层面的四步操作

    ----------- 1.存储文件属性.内核先找到一个空的i节点,并把文件属性信息记录其中.   2.存储数据.先计算要多少个磁盘块,在内核自由块列表找到合适数量的磁盘块,并把数据从内核的缓冲区依次 ...

  9. 多线程/进度条应用(progressbar)

    使用Control Sets 下的 ProgressBar - Responsive Loop控件 ProcessBar 或者 CancelBar 都可以被设置为 invisible 代码如下(分享自 ...

  10. ABAP屏幕基础

    Select语句的使用 关键字into后可以加 structure(结构体), internal table(内表) 和 fieldlist(字段列表) Authority 权限 程序员可以根据权限对 ...