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. Django 使用原生SQL

    def dictfetchall(cursor): "将游标返回的结果保存到一个字典对象中" desc = cursor.description return [ dict(zip ...

  2. ERP存储过程

    [dbo].[st_MES_MonitorMachine] -------------------------------------------- USE [ChiefMESNew]GO /**** ...

  3. SASS优化响应式断点管理

    前端开发whqet,csdn,王海庆,whqet,前端开发专家 原文:<Managing Responsive Breakpoints with Sass> 作者:Hugo Giraude ...

  4. Java模拟登陆【转载】

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  5. js实现自己定义鼠标右键-------Day45

    又是周末了,只是事实上这在国外应该算是一周的開始吧,无论怎么说,今天是在歇息,放松我紧绷的神经,放松我有些疲惫的精神,昨晚上要裂了般的头疼,仿佛全部的数据都在脑子字面飞舞旋转,伴着一阵阵的恶心,当时把 ...

  6. xtrabackup原理1

    http://www.cnblogs.com/Amaranthus/archive/2014/08/19/3922570.html Percona XtraBackup User Manual 阅读笔 ...

  7. 高并发网络编程之epoll详解

    select.poll和epoll的区别 在linux没有实现epoll事件驱动机制之前,我们一般选择用select或者poll等IO多路复用的方法来实现并发服务程序.在大数据.高并发.集群等一些名词 ...

  8. qsort函数、sort函数 (精心整理篇)

    先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...

  9. Codeblocks支持C++11

    Setting->Compiler 直接在“Have g++ follow the C++11 ISO C++ language standard [-std=c++11]” 选项上打勾 保存就 ...

  10. 高级I/O之readn和writen函数

    管道.FIFO以及某些设备,特别是终端.网络和STREAMS设备有下列两种性质: (1)一次read操作所返回的数据可能少于所要求的数据,即使还没有达到文件尾端也可能是这样.这不是一个错误,应当继续读 ...