【杨氏矩阵+勾长公式】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 ...
随机推荐
- BestCoder Round #67 (div.2) N bulbs(hdu 5600)
N bulbs Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- IPhone 设备状态、闪光灯状态
//判断闪光灯状态,修改默认的"CameraFlashOff" 按钮图片.转由 TGCameraFlash.m 控制图标切换 AVCaptureDevice *device ...
- dns解析对SEO产生的影响
DNS 是域名系统 (Domain Name System) 的缩写,它是由解析器和域名服务器组成的.域名服务器是指保存有该网络中所有主机的域名和对应的IP地址,并具有将域名转换为IP地址功能的服务器 ...
- C#枚举数值与名称的转换
在应用枚举的时候,时常需要将枚举和数值相互转换的情况.有时候还需要转换成相应的中文.下面介绍一种方法. 首先建立一个枚举: /// <summary> /// 颜色 /// </su ...
- 菜鸟学习 git
到新公司学习和使用 git 有一段时间了.不得不说 git 真的很牛逼,当然,git 的牛逼是建立在 Linux 之父的牛逼的基础上的. 首先跪着推荐 git 学习网站:http://www.liao ...
- Windows 8 之 windbg 配置
怎么安装windbg? 在Win8中,要通过安装windows 8 SDK来安装. 安装之后,在C:\Program Files (x86)\Windows Kits\8.0\Debuggers\x6 ...
- word2010 ctrl v not work
终于解决了word 2010中ctrl v 不能用的问题. 0 word ctrl c 可以用,右键粘贴可以正常使用,快捷键ctrl v不能用. 1 在excel中ctrl c 和ctrl v,可以正 ...
- 教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
(40)Take advantage of .NET 4.5 async constructs 招数40: 利用.NET 4.5异步结构 With the arrival of .NET 4.5, w ...
- cocos2d-x CCTableView
转自:http://www.cnblogs.com/dcxing/archive/2013/01/16/2862068.html CCTableView在游戏中一般用在背包这样场景或层中,当然也不止这 ...
- Posting data to a HttpHandler greater then ~29MB gives a 404 error
1down votefavorite 1 I am testing a HttpHandler that accepts XML. It works fine when a small amount ...