POJ2279总结

Mr. Young’s Picture Permutations

题目来源

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

思路

这一题要用到杨氏矩阵钩子定理。另外在处理阶乘时,中间变量一定要定义为long long。

#include<stdio.h>
#include<string.h>
int n,tot;
long long int temp1,temp2;
int row[36];
int hook[36];

int gcd(int a, int b){
	return b==0?a:gcd(b,a%b);
}

int main(){
	while (scanf("%d",&n)&&n!=0){
		tot=0;
		temp1=1;
		temp2=1;
		memset(hook,0,sizeof(hook));
		for (int i=1;i<=n;i++){
			scanf("%d",&row[i]);
		}
		for (int i=n;i>=1;i--){
			for (int j=1;j<=row[i];j++){
				tot++;
				for (int k=i+1;k<=n;k++){
					if (j<=row[k]){
						hook[tot]++;
					}else {
						break;
					}
				}
				hook[tot]+=row[i]-j+1;
			}
		}
		for (int i=1;i<=tot;i++){
			temp1*=i;
			temp2*=hook[i];
			int temp=gcd(temp1,temp2);
			temp1=temp1/temp;
			temp2=temp2/temp;
		}
		printf("%lld\n",temp1/temp2);
	}
	return 0;
}

我觉得这一题能用五维数组做,爆空间的问题能用一维数组模拟五维来解决,先标记,写出来之后补充。

2020年10月9日补充:

我不知道看了谁的解法说直接开五维数组dp会爆空间的,搞得我先去用一维数组模拟了五维之后才发现其实直接开五维数组也是可以的。

#include<stdio.h>
#include<string.h>

int n,tot;
long long int dp[400000];
int row[36];

int getPosition(int a,int b,int c,int d,int e){
	return (16*11*8*7)*a
		+(11*8*7)*b
		+(8*7)*c
		+7*d
		+e;
}
int check(int a,int b,int c,int d,int e){
	return a>=b&&b>=c&&c>=d&&d>=e?1:0;
}
int main(){
	memset(dp,0,sizeof(dp));
	dp[getPosition(0,0,0,0,0)]=1;
	for (int i=0;i<=30;i++){
		for (int j=0;j<=i&&i+j<=30;j++){
			for (int k=0;k<=j&&i+j+k<=30;k++){
				for (int t=0;t<=k&&i+j+k+t<=30;t++){
					for (int m=0;m<=t&&i+j+k+t+m<=30;m++){
						if (i&&check(i-1,j,k,t,m)){
							dp[getPosition(i,j,k,t,m)]+=dp[getPosition(i-1,j,k,t,m)];
						}
						if (j&&check(i,j-1,k,t,m)){
							dp[getPosition(i,j,k,t,m)]+=dp[getPosition(i,j-1,k,t,m)];
						}
						if (k&&check(i,j,k-1,t,m)){
							dp[getPosition(i,j,k,t,m)]+=dp[getPosition(i,j,k-1,t,m)];
						}
						if (t&&check(i,j,k,t-1,m)){
							dp[getPosition(i,j,k,t,m)]+=dp[getPosition(i,j,k,t-1,m)];
						}
						if (m){
							dp[getPosition(i,j,k,t,m)]+=dp[getPosition(i,j,k,t,m-1)];
						}
					}
				}
			}
		}
	}
	while (scanf("%d",&n)&&n!=0){
		memset(row,0,sizeof(row));
		for (int i=1;i<=n;i++){
			scanf("%d",&row[i]);
		}
		printf("%lld\n",dp[getPosition(row[1],row[2],row[3],row[4],row[5])]);
	}
	return 0;
}

发表评论