UVALive 5971

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

 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 }

UVALive 5971,布布扣,bubuko.com

时间: 2024-08-27 10:36:43

UVALive 5971的相关文章

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

BZOJ 2226: [Spoj 5971] LCMSum( 数论 )

∑lcm(i,n) = ∑ i*n/(i,n) = ∑d|n∑(x,n)=d x*n/d = ∑d|n∑(t,n/d)=1t*n = n∑d|nf(d). f(d)表示1~d中与d互质的数的和, 即f(d) = d*φ(d)/2(d>=2). 然后O(n)筛φ, 每次询问暴力算即可...最大是100w,sqrt(100w)=1000内的质数是168个, 所以复杂度是O(n + T*168), 可以AC  ----------------------------------------------

UVALive 5545 Glass Beads

Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 554564-bit integer IO format: %lld      Java class name: Main Once upon a time there was a famous actress. As you may expect, she played mostly