排列问题 permutation(似乎原题是SRM 592 Little Elephant And Permutation Div1?)
来源:互联网 发布:数据分析理论与方法 编辑:程序博客网 时间:2024/06/03 21:31
排列问题 permutation
题目描述
长度为N的排列是一个序列
对于两个排列a和b,定义
给定整数N和K,求有多少对排列a和b 满足a和b的长度都为N,并且
输入格式
第1行:2个整数N和K
输出格式
第1行:1个整数,表示答案,答案模1,000,000,007
输入样例
2 4
输出格式
2
样例说明
2个长度为2的排列一共有4种情况:
* magic( (1,2), (1,2) ) = 1+2 = 3
* magic( (1,2), (2,1) ) = 2+2 = 4
* magic( (2,1), (1,2) ) = 2+2 = 4
* magic( (2,1), (2,1) ) = 2+1 = 3
magic值大于等于4的有2对排列。
数据范围
50%的数据
*
100%的数据
*
*
限制
- 时间:
1 S - 空间:
128 M
我这个措起辞来狗屁不通毫无逻辑的人竟然会写一篇我自己都是看着题解才勉强过掉老师出的随机数据的题....
先附一段官方题解:
本来打算把老师给的pdf的内容粘上去结果发现似乎很难然后又懒得开lantern懒得翻译所以就凑合看吧..
A good idea is then to guarantee that we decide the positions for the numbers in an order. The
best order is decreasing instead of increasing. If in the example above, x is guaranteed to be
smaller than a , b and c, then it is easy to see that whenever x is made to share row with another
number, the magic will not change. Instead, if x is put in rows that were previously empty, the
score will increase by x for each row of that kind that is picked.n: There are n numbers for which we didn’t decide a position yet. This means that the next number is n.
k: The requirement. The remaining decisions need to increase the magic number by at least k.
s: The number of empty columns.
o_a: The number of cells in the top that have the respective bottom cell filled. The number of
occupied cells in the top.
o_b: The number of occupied cells in the bottom.Dynamic programming
The recurrence relation is acyclic because n always decreases. We can use dynamic programming
so that each combination of parameters is evaluated exactly once. There will be a couple of issues
though.(N <= 50)
,(K <= 2500)
and there appear to beO(KN^4 )
combinations of
parameters. This complexity is great for the time limit but not so good for the memory limit. We
need more work.Occupied cells
Notice that in all steps in the recurrence(o_a = o_b)
. Whenevero_a
is increased so iso_b
and they also decrease in the same way. So we can just replace themo_a
ando_b
by a
single value o.
f(n,k,s,o)=s*f(n-1,max(0,k-n),s-1,o)+s*(s-1)*f(n-1,max(0,k-2n),s-2,o+1)+o^2*f(n-1,k,s,o-1)+2*o*s*f(n-1,max(0,k-n),s-1,o)
This cuts the estimated complexity toO(K N^3)
然而我并!没!有!用这种方法去写…2333
先占个坑吧..说不定以后什么时候就填上了呢QWQ
然后再介绍一种题解(当时我对着这个题解推了半天)
我们发现可以先固定排列a为(1,2,…,n),最后将答案乘以n!即可。
我们可以设计状态f[i][j][k]表示已经在b中填充了数1到i,填在下标为i+1到n的数有j个,满足max(a[p],b[p])<=i的max(a[p],b[p])的和为k,这样的的填充方法数。
于是可以分类:
1.将数i填在下标为i处,则k+=i,j不变.
2.将数i填在下标小于i处,下标为i处填入了小于i的数,则k+=2 * i, j–.
3.将数i填在下标小于i处,下标为i处填入了大于i的数,则k+=i,j不变.
4.将数i填在下标大于i处,下标为i处填入了大于i的数,则k不变,j++.
5.将数i填在下标大于i处,下标为i处填入了小于i的数,则k+=i,j不变.
最终答案为f[n][0][x]的和,其中x>=k.
然后下为我几乎看着(老师给的)标程想了半天后才过掉老师出的随机数据的代码..
#include<cstdio>#define mod 1000000007#define min(a,b) (a)<(b)?(a):(b)int n,m,ans,f[55][55][2505];long long c;int main(){ freopen("permutation.in","r",stdin); freopen("permutation.out","w",stdout); int i,j,k; scanf("%d%d",&n,&m); f[0][0][0]=1; for(i=1;i<=n;i++) for(j=0;j<=i;j++) for(k=0;k<=m;k++) { c=f[i-1][j][k]; f[i][j][min(k+i,m)]=(f[i][j][min(k+i,m)]+((2*j+1)*c)%mod)%mod;//1,3,5 f[i][j+1][k]=(f[i][j+1][k]+c)%mod;//4 if(j) f[i][j-1][min(m,k+i*2)]=(f[i][j-1][min(m,k+i*2)]+(c*j*j)%mod)%mod;//2 } c=f[n][0][m]; for(i=1;i<=n;i++) c=(c*i)%mod; ans=c; printf("%d\n",ans);}
接着详细介绍(其实详细不到哪去…..?)
根据上文,1,3,5其实是可以合并的..
然后,再加上各自的方案数..
具体的推导过程我也不是特别清楚所以先占个坑等完全想清楚再填…
- 排列问题 permutation(似乎原题是SRM 592 Little Elephant And Permutation Div1?)
- 排列问题(permutation)
- bc7 Little Pony and Permutation
- 排列(permutation)
- 排列(permutation)
- 排列(permutation)
- HDOJ题目4985Little Pony and Permutation(置换群)
- HDU 4985 Little Pony and Permutation(数学 置换群)
- 杭电4985 Little Pony and Permutation(水置换)
- HDU 4985 Little Pony and Permutation(简单模拟)
- hdu 4985 Little Pony and Permutation(水题)
- HDU 4985-Little Pony and Permutation(模拟置换)
- Codeforces 553B Kyoya and Permutation 排列问题
- Java练习:排列(Permutation)问题
- hdu 4985 Little Pony and Permutation
- hdu 4985 Little Pony and Permutation(置换)
- HDU 4985 Little Pony and Permutation(置换)
- HDU 4985 Little Pony and Permutation
- jsp基础(一)
- 关于netty的HttpUtil.isTransferEncodingChunked(HttpMessage)方法已过时解决方案
- SQLserver创建数据库
- 搭建私有CA
- LeetCode 540:Single Element in a Sorted Array
- 排列问题 permutation(似乎原题是SRM 592 Little Elephant And Permutation Div1?)
- [LeetCode]54. Spiral Matrix
- Html入门
- 2018京东笔试 疯狂序列 神奇数
- swap-nodes-in-pairs
- Android Z轴布局
- java的类加载机制
- iOS 内存管理~自定义对象的拷贝
- MIT 的高级数据结构课程主页-内有视频链接