人生有信仰 数据有梯度 暴力不爆零


bzoj 2483: Pku2279 Mr. Young's Picture Permutations -- 钩子公式

2483: Pku2279 Mr. Young's Picture Permutations

Time Limit: 1 Sec  Memory Limit: 128 MB

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.

杨先生要给他的学生们拍照片。首先,站队状况是确定的,若第i行站了a[i]个人,则有a[1] ≥ a[2] ≥ ... ≥ a[n],所有的行都是靠左对齐的。并且,在同一行和同一列里面,从左到右或者从上到下身高都是递减的。现在所有学生的身高都不相同,学生数不超过30个,行数不超过5。你需要求出所有可能的排队方法的总数。

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

HINT

先介绍一下什么是杨氏矩阵Young tableau//允许我转载一下 QwQ   出处:https://www.zhihu.com/question/52335435/answer/130097881

杨氏矩阵是这样一类带限制的矩阵:
  1. 矩阵里同一行的元素,左边的元素小于右边的元素。
  2. 矩阵里同一列的元素,上边的元素小于下边的元素。

而问题中所求的是排列,可以双射到它的逆排列,统计逆排列的个数,具体来说,就是求(i,j)在排列里的排名,从而转化为将1N填充到矩阵中,问有多少种方法满足杨氏矩阵的限制。

固定矩阵形态,求有多少种本质不同的杨氏矩阵,这个问题是非常经典的,可以使用钩子公式Hook length formula求解。
定义h_{\lambda}(i,j)表示矩阵里满足i=x,j\leq yi\leq x,j=y(x,y)位置个数,则本质不同的杨氏矩阵个数是\frac{N!}{\prod_{i,j}{h_{\lambda}(i,j)}}

那么对于这个问题,满足条件的排列数就是\frac{N!}{\prod_{i = 0}^{m - 1}{\frac{(n+i)!}{i!}}},是O(\exp(N\log N))的。
 
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,a[6],sum,ji[33];
bool mp[33][33];
ll ans=1;
ll ksm(ll a,int b)
{
    ll s=1;
    for(;b;b>>=1,a*=a) if(b&1) s*=a;
    return s;
}
int main()
{
    int i,j,h,k;
    while(n=read())
    {
        ans=1;sum=0;
        memset(ji,0,sizeof(ji));
        memset(mp,0,sizeof(mp));
        for(i=1;i<=n;i++)
        {
            a[i]=read();sum+=a[i];
            for(j=1;j<=a[i];j++) mp[i][j]=1;
        }
        for(i=1;i<=sum;i++)
        {
            int tp=i;
            for(j=2;j<=tp;j++)
                while(tp%j==0) tp/=j,ji[j]++;
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=a[i];j++)
            {
                h=a[i]-j;
                for(k=i;k<=n;k++) if(mp[k][j]) h++;
                int tp=h;
                for(k=2;k<=tp;k++)
                    while(tp%k==0) tp/=k,ji[k]--;
            }
        }
        for(i=2;i<=30;i++) ans*=ksm((ll)i,ji[i]);
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2017-04-27 16:19  lkhll  阅读(301)  评论(0编辑  收藏  举报