【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations
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 题意:给出行数k,以及每行数字的个数n[i],问一共有多少种排列方法使元素从左到右从上到下依次递减。(即构成一个杨氏矩阵)。
分析:勾长公式。暴力+公式;
杨氏矩阵(杨表)(面试会问到)
杨表由有限的方格组成。
对于一个正整数,给定一个整数分拆λ(10=1+4+5),则对应一个杨表(注意这是一个递降的过程,也就是说下面一行的方格数要大于等于上一行的方格数)。

一个(1,4,5)分拆表示的杨表
杨表与整数分拆λ一一对应。
给定一个杨表,一共有n个方格。那么把1到n这n个数字填到这个杨表中,使得每行从左到右都是递增的,每列从下到上也是递增的。如图

一个杨表的表示
【勾长】对于杨表中的一个方格v,其勾长hook(v)等于同行右边的方格数加上同列上面的方格数,再加上1(也就是他自己)。
【勾长公式】用表示杨表个数,则

对于分拆10 = 5 + 4 + 1 的应的杨表. 因此共有
种方法。
公式题。求出同行右边的方格数+同列上面的方格数+1。唯一注意的地方是最后除的时候要防止溢出。
LL ans = ;
for(int i = ; i <= tot; i++)
{
factor[i] = i;
for(int j = ; j <= tot; j++)
{
int tmp = gcd(factor[i], young[j]);
factor[i] /= tmp;
young[j] /= tmp;
}
}
//经过上述处理后young[i]均变为1,至于原因,分子除分母是整数,结果分母一定会变为1。
for(int i = ; i <= tot; i++)
{
ans *= factor[i];
}
代码:
/*
Problem: poj_2279
tags: 杨氏矩阵+勾长公式
*/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define mems(x, t) memset(x, t, sizeof(x));
using namespace std;
typedef long long LL;
const int maxn = ;
const int INF = 0x3f3f3f3f; int k, tot, n[maxn];
int table[maxn][maxn], young[], factor[];
LL gcd(LL a, LL b)
{
return b == ? a : gcd(b, a%b);
}
int main()
{
while(~scanf("%d", &k) && k)
{
mems(table, );
tot = ;
for(int i = ; i <= k; i++)
{
scanf("%d", &n[i]);
tot += n[i];
for(int j = ; j <= n[i]; j++)
{
table[i][j] = ;
}
}
mems(young, );
int cnt = ;
for(int i = ; i <= k; i++)
{
for(int j = ; j <= n[i]; j++)
{
if(table[i][j] == )
{
young[cnt]++;
for(int l = i+; l <= k; l++)
{
if(!table[l][j]) break;
young[cnt]++;
}
young[cnt] += n[i]-j;
cnt++;
}
}
}
LL ans = ;
for(int i = ; i <= tot; i++)
{
factor[i] = i;
for(int j = ; j <= tot; j++)
{
int tmp = gcd(factor[i], young[j]);
factor[i] /= tmp;
young[j] /= tmp;
}
}
for(int i = ; i <= tot; i++)
{
ans *= factor[i];
}
printf("%lld\n", ans);
}
return ;
}
【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations的更多相关文章
- 轮廓线DP:poj 2279 Mr. Young's Picture Permutations
poj 2279 Mr. Young's Picture Permutations \(solution:\) 首先摘取一些关键词:(每行不超过它后面的行)(每排学生安排高度从左到右减少)(学生的高度 ...
- [POJ 2279] Mr. Young's Picture Permutations
[题目链接] http://poj.org/problem?id=2279 [算法] 杨氏矩阵与勾长公式 [代码] #include <algorithm> #include <bi ...
- POJ P2279 Mr. Young's Picture Permutations 题解
每日一题 day14 打卡 Analysis 五维dpf[a1,a2,a3,a4,a5]表示各排从左端起分别占了a1,a2,a3,a4,a5个人时合影方案数量然后我们枚举a1,a2,a3,a4,a5从 ...
- bzoj 2483: Pku2279 Mr. Young's Picture Permutations -- 钩子公式
2483: Pku2279 Mr. Young's Picture Permutations Time Limit: 1 Sec Memory Limit: 128 MB Description ...
- POJ2279 Mr Young's Picture Permutations
POJ2279 Mr Young's Picture Permutations 描述: 有N个学生合影,站成左对齐的k排,每行分别有N1,N2…NK个人,第一排站最后,第k排站之前.学生身高依次是1… ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
- Mr. Young's Picture Permutations
Mr. Young's Picture Permutations 给出一个有k列的网格图,以及每列图形的高度\(n_i\),下端对齐,保证高度递减,设有n个网格,询问向其中填1~n保证每行每列单调递增 ...
- poj2279 Mr. Young's Picture Permutations[勾长公式 or 线性DP]
若干人左对齐站成最多5行,给定每行站多少个,列数从第一排开始往后递减.要求身高从每排从左到右递增(我将题意篡改了便于理解233),每列从前向后递增.每个人身高为1...n(n<=30)中的一个数 ...
- poj2279——Mr. Young's Picture Permutations
Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...
随机推荐
- JAVA中的常见面试题1
1.线程同步的方法的使用. sleep():使一个正在运行的线程处于睡眠状态,是一个静态方法,调用此方法要捕捉InterruptedException异常. wait():使一个线程处于等待状态,并且 ...
- Spring AOP Interceptor transaction is not working
Problem The Spring AOP transaction is not working in following interceptors? <bean id="testA ...
- ural 1303 Minimal Coverage(贪心)
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...
- CommandLine 和 Options
用到的jar包 <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli& ...
- python 循环
200 ? "200px" : this.width)!important;} --> 介绍 python中有两种循环,分别是for...in循环.while循环:for.. ...
- Python魔术师--self
(原文是 Python's Magical Self ,来自 http://concentricsky.com ) Python的self参数有时真让人抓狂,比如,你必须在每一个类的方法里显示定义se ...
- java的BigDecimal
java的BigDecimal 一般设计到高精度的加法或乘法或者阶乘的求和积都会用到BigDecimal这个类. import java.util.*;import java.math.BigDeci ...
- [0.0]Analysis of Baidu search engine
Rencently, my two teammates and I is doing a project, a simplified Chinese search engine for childre ...
- secureCRT使用VIM时对语法高亮
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- MySQL几个注意点
1.在创建表.对表进行操作之前,必须首先选择数据库.通过 mysql_select_db() 函数选取数据库.当您创建 varchar 类型的数据库字段时,必须规定该字段的最大长度,例如:varcha ...