Description
- 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
Output
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:
上下颠倒的杨氏图表 ,利用杨氏图表+钩子公式过题。
杨氏矩阵(杨氏图表) 定义(需满足的条件/特征):
(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> using namespace std; const int maxn=30; typedef long long ll; ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); } int n,cnt,a[50]; ll sum[10010],x,y; int main(){ while(scanf("%d",&n)==1,n){ memset(sum,0,sizeof(sum)); x=1,y=1,cnt=0; for (int i=1; i<=n; i++) scanf("%d",&a[i]); for (int i=1; i<=n; i++){ for (int j=1; j<=a[i]; j++){ cnt++; for (int k=i+1; k<=n; k++){ if(a[k]>=j) sum[cnt]++; else break; } sum[cnt]+=a[i]-j+1; } } for (int i=1; i<=cnt; i++){ x*=i,y*=sum[i]; int k=gcd(x,y); x/=k,y/=k; } printf("%lld\n",x/y); } return 0; }
Description
- 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
Output
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:
上下颠倒的杨氏图表 ,利用杨氏图表+钩子公式过题。
杨氏矩阵(杨氏图表) 定义(需满足的条件/特征):
(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> using namespace std; const int maxn=30; typedef long long ll; ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); } int n,cnt,a[50]; ll sum[10010],x,y; int main(){ while(scanf("%d",&n)==1,n){ memset(sum,0,sizeof(sum)); x=1,y=1,cnt=0; for (int i=1; i<=n; i++) scanf("%d",&a[i]); for (int i=1; i<=n; i++){ for (int j=1; j<=a[i]; j++){ cnt++; for (int k=i+1; k<=n; k++){ if(a[k]>=j) sum[cnt]++; else break; } sum[cnt]+=a[i]-j+1; } } for (int i=1; i<=cnt; i++){ x*=i,y*=sum[i]; int k=gcd(x,y); x/=k,y/=k; } printf("%lld\n",x/y); } return 0; }