码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 5971

时间:2014-07-22 23:22:27      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   width   

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
bubuko.com,布布扣
 1 #include <map>
 2 #include <set>
 3 #include <list>
 4 #include <cmath>
 5 #include<cctype>
 6 #include <ctime>
 7 #include <deque>
 8 #include <stack>
 9 #include <queue>
10 #include <cstdio>
11 #include <string>
12 #include <vector>
13 #include<climits>
14 #include <cstdlib>
15 #include <cstring>
16 #include <iostream>
17 #include <algorithm>
18 #define LL long long
19 #define PI 3.1415926535897932626
20 using namespace std;
21 int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
22 #define MAXN 1000005
23 #define MOD 1000000007
24 LL ans[MAXN],tmp[MAXN];
25 void init()
26 {
27     ans[1]=1;ans[2]=1;
28     for (int i=3;i<MAXN;i++)
29        {
30            ans[i]=((i-1)*ans[i-1])+(i-2)*ans[i-2];
31            ans[i]%=MOD;
32        }
33 }
34 int main()
35 {
36     init();
37     int T;int kase=1;
38     scanf("%d",&T);
39     while (T--)
40     {
41         int N;
42         scanf("%d",&N);
43         printf("Case %d: %lld\n",kase++,ans[N]);
44     }
45     return 0;
46 }
View Code

 

 

UVALive 5971,布布扣,bubuko.com

UVALive 5971

标签:style   blog   http   color   os   width   

原文地址:http://www.cnblogs.com/qscqesze/p/3861467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!