POJ 2279 Mr. Young's Picture Permutations(杨氏矩阵和钩子公式)

来源:互联网 发布:whatsapp mac 编辑:程序博客网 时间:2024/05/21 19:35
Mr. Young's Picture Permutations
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 1165
Accepted: 576

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 XX X XX X XX

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 1012                 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 14645  46  35  36  34  36  34  35  25  26  24  26  24  25  26  256   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

13051 1 1 1 133 2 145 3 3 156 5 4 3 2215 150

Sample Output

111641581418926089694845

Source

Greater New York 2004
题目大意:
  给出n行,每行有人数限制num[i],并且num[i]>=num[i+1],总人数暂且称为tot=∑num[i],把1~tot这些数字填入矩阵,使得矩阵满足每行单调递增,每列单调递增,求满足要求的矩阵数目

知识点

杨氏矩阵又叫杨氏图表,它是这样一个矩阵,满足条件:

 

(1)如果格子(i,j)没有元素,则它右边和上边的相邻格子也一定没有元素。

(2)如果格子(i,j)有元素a[i][j],则它右边和上边的相邻格子要么没有元素,要么有元素且比a[i][j]大。

 

1 ~ n所组成杨氏矩阵的个数可以通过下面的递推式得到:

 

 

如图就是n=3时的杨氏矩阵。



下面介绍一个公式,那就是著名的钩子公式

对于给定形状,不同的杨氏矩阵的个数为:n!除以每个格子的钩子长度加1的积。其中钩子长度定义为该格子

右边的格子数和它上边的格子数之和。

钩子公式:res=n!  /  (hock[1]*hock[2]*.....hock[n]);

hock[i]=在其上方和右方的所有个数+1;

AC代码

#include<cstdio>  #include<cstring>  int gcd(int a,int b)  {      return b==0?a:gcd(b,a%b);  }   int d[36];  int num[36];  int main()  {      int n;      while(scanf("%d",&n)&&n)      {      for(int i=1;i<=n;i++)      scanf("%d",&d[i]);      int tot=0;      memset(num,0,sizeof(num));      for(int i=n;i>=1;i--)      {          for(int j=1;j<=d[i];j++)          {              tot++;              for(int k=i+1;k<=n;k++)              if(d[k]>=j)              num[tot]++;              else              break;              num[tot]+=d[i]-j+1;          }         }      long long int t1=1,t2=1;      for(int i=1;i<=tot;i++)      {          t1*=i;           t2*=num[i];          int t=gcd(t1,t2);          t1/=t;          t2/=t;      }      printf("%lld\n",t1/t2);     }     return 0;  }  





原创粉丝点击