您的位置:首页 > 其它

UVALive 5971

2014-07-22 21:40 176 查看
Problem J Permutation Counting
Dexter considers a permutation of first N natural numbers good if it doesn't have x and x+1 appearing consecutively, where (1 ≤ x < N) For example, for N=3 , all goodpermutations 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 ≤ 106)
.
Output
For each case, print the case number and the number ofgoodpermutations
modulo1000 000 007
.
Sample Input
Output for Sample Input
3
2
3
5
Case 1: 1
Case 2: 3
Case 3: 53

#include <map>
#include <set>
#include <list>
#include <cmath>
#include<cctype>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include<climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 1000005
#define MOD 1000000007
LL ans[MAXN],tmp[MAXN];
void init()
{
ans[1]=1;ans[2]=1;
for (int i=3;i<MAXN;i++)
{
ans[i]=((i-1)*ans[i-1])+(i-2)*ans[i-2];
ans[i]%=MOD;
}
}
int main()
{
init();
int T;int kase=1;
scanf("%d",&T);
while (T--)
{
int N;
scanf("%d",&N);
printf("Case %d: %lld\n",kase++,ans
);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: