首页 > 专题系列 > Java解POJ > POJ 2279 Mr. Young’s Picture Permutations [解题报告] Java
2013
11-11

POJ 2279 Mr. Young’s Picture Permutations [解题报告] Java

Mr. Young’s Picture Permutations

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

1
1
16
4158
141892608
9694845

解题代码:

//* @author: ccQ.SuperSupper
import java.math.*;
import java.util.*;

public class Main {

/**
 * @param args
 */
public static void main(String[] args) throws Exception{
  // TODO Auto-generated method stub

  BigInteger  num ,b;//= new BigInteger;
  num=BigInteger.valueOf(1);
		
  Scanner cin = new Scanner(System.in);
		
   int n,i,j,sum,t;
   int way[] = new int[100];
   int flag[][] = new int[100][100];
		
   while(cin.hasNext())
   {
	n = cin.nextInt();
	if(n==0) break;
	for(i=1,sum=0;i<=n;++i)
	{
		way[i]=cin.nextInt();
		sum+=way[i];
	}
		
	num = BigInteger.valueOf(1);
		
	for(i=2;i<=sum;++i)
	{
		b = BigInteger.valueOf(i);
		num = num.multiply(b);
	}
			
	//System.out.println(num);
	for(i=1;i<=n;++i) for(j=1;j<=way[i];++j)
	{
		sum=way[i]-j;
		for(t=i+1;t<=n;++t) if(way[t]>=j) sum++;
				
		sum++;
		b = BigInteger.valueOf(sum);
		//System.out.println(b);
		num = num.divide(b);
	}
			
	System.out.println(num);
			
    }
 }

}

  1. 是穷举,但是代码有优化(v数组),并不是2^n。测试数据应该没问题,之前有超时的代码。

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  3. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的