UVAlive 5971 Permutation Counting
来源:互联网 发布:勃列日涅夫知乎 编辑:程序博客网 时间:2024/05/19 19:58
Permutation Counting
Dexter considers a permutation of first N natural numbers good if it doesn't have x and x+1 appearing cons ecutively, where (1 ≤ x < N). For example, for N=3, all good permutations are:
1.{1, 3, 2}
2.{2, 1, 3}
3.{3, 2, 1}
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.Each case starts with a line containing an integer N (1 ≤ N ≤ 10^6).
Output
For each case, print the case number and the number of good permutations modulo 1000000007.
Sample Input
3
2
3
5
Sample Output
Case 1: 1
Case 2: 3
Case 3: 53
Dexter considers a permutation of first N natural numbers good if it doesn't have x and x+1 appearing cons ecutively, where (1 ≤ x < N). For example, for N=3, all good permutations are:
1.{1, 3, 2}
2.{2, 1, 3}
3.{3, 2, 1}
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.Each case starts with a line containing an integer N (1 ≤ N ≤ 10^6).
Output
For each case, print the case number and the number of good permutations modulo 1000000007.
Sample Input
3
2
3
5
Sample Output
Case 1: 1
Case 2: 3
Case 3: 53
解析
这道题是一道推公式的数学题 f[i]表示n=i的good permutations
f[i]=f[i-1]*(i-1)+f[i-2]*(i-2)
其中f[i-1]*(i-1)很容易想到对于i-1合法排列插入i(只要i不插在i-1后就可以)这样的空位总共有i-1个
例如对于n=4
_1_3 2_
_2_1_3
_3 2_1_
后面的f[i-2]*(i-2)稍稍难一点,我们可以考虑有一组x x+1相连的情况,只要把 i 插在x x+1之间就合法了。比如考虑n=5 对于2 3 1 4这样一组非法的序列在2 3间插入4就合法了。这时候计算总共有多少个仅有2 3相连的非法序列:
因为仅有2 3相连所以把2 3看做一个整体(我们姑且把这个整体看做2)且要保证这个整体前面不是1后面不是4然后再变换一下:2 3整体看做2 比3大的数减1(这样4就变成了3 就不能排在2后面了 换句话说2 3 4肯定不会连在一起)问题就转化成了求i-2的合法序列数。
2 3 1 4
2 1 3
像x x+1这样的在i-1中能组成i-2对,所以f[i-2]*(i-2)
这样就推出f[i]=f[i-1]*(i-1)+f[i-2]*(i-2)
#include<cstdio>using namespace std;long long dp[1000005];int main(){dp[1]=1;dp[2]=1;for(int i=3;i<=1000000;i++)dp[i]=(dp[i-1]*(i-1)%1000000007+dp[i-2]*(i-2)%1000000007)%1000000007;int T;scanf("%d",&T);for(int i=1;i<=T;i++){int a;scanf("%d",&a);printf("Case %d: %lld\n",i,dp[a]);}return 0;}
0 0
- UVAlive 5971 Permutation Counting
- UVAlive 5971 Permutation Counting(数论 dp)
- uvalive 5092 - Permutation Counting
- Permutation Counting HDU3664 UVALive
- UVALive 5971 Permutation Counting 组合学、递推
- HDOJ 3664 Permutation Counting / UVALive 5092 DP
- UVALive 5971 (LA 5971) Permutation Counting 动态规划 (容斥原理超时)
- UVALive 6508 Permutation Graphs
- hdoj 3664 Permutation Counting
- HDOJ 3664 Permutation Counting
- UVA - 1485 Permutation Counting
- HDU 3664 Permutation Counting
- HDOJ 3664 Permutation Counting
- HDU 3667 Permutation Counting
- LA 5092 Permutation Counting
- LA 5092 Permutation Counting
- UVALive 6527 Counting ones
- UVALive 3295 Counting Triangles
- 【工具】Java工程导入Hibernate
- 图论--最小生成树和最短路1
- 进程内存布局
- Spring 注解学习(一) 构建简单Web应用
- liferay通过ldap同步用户([用户组])设置(一)
- UVAlive 5971 Permutation Counting
- 如何找到你的泰国女友在线
- 多才多艺的煅烧高岭土查找在工业部门的应用
- hdu多校联合Peter's Hobby
- 拓拔排序算法
- Android 屏幕适配学习
- 全自动软化水设备:软化水装置工作步骤指南
- 培训指南对于考 - 701考试
- 固体油墨打印机