链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2321
标题:permunation
题目描述:
n个人排成一队,队头的人编号为1,后面的人编号分别为2,3,…,n. 1号人前面没有人,i号人的前面是i-1。 现在Kim想让这n个人重新排队,要求重新排队后编号为i的人前面不能是i-1。问有多少种排队的方法。方案数可能很大,输出答案模1e9+7。
Input
第一行一个整数T,表示有T组数据。
接下来T行,每行一个整数n。表示有n个人排成一队。
Output
对于每个n,输出答案 mod 1000000007。
Sample Input
2 1 4
Sample Output
1 11
Hint
T<=2000
n<=2000000
n=4 时,原队列为 1 2 3 4。所有合法的新队列如下:
4 1 3 2
4 3 2 1
4 2 1 3
3 2 1 4
3 2 4 1
2 1 4 3
2 4 3 1
2 4 1 3
3 1 4 2
1 3 2 4
1 4 3 2
一共有11种。
题意:有t组输入,每组有n个人,他们的编号由1到n,题目让问的是总共有多少种排序的方式,满足第n个数字减去第n-1‘个数字不等于1,即假设 第4个数字是6的话,则第3个数字不能为5
思路:根据n=1时,a1=1 ,
n=2时,a2=1;
n=3时,a3=3;
n=4时,a4=11;
n=5时,a5=53;
推出公式: an=(an-1)*(n-1)+(an-2)*(n-2),再对le9+7取模打表就行了
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int t,n;
long long a[2000001];
void f()
{
for(int i=3;i<=2000001;i++)
a[i]=((i-1)*a[i-1]+(i-2)*a[i-2])%1000000007;
}
int main()
{
a[1]=1;
a[2]=1;
f();
cin>>t;
while(t--)
{
scanf("%d",&n);
printf("%lld\n",a[n]);
}
return 0;
}