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
View Code
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
相关文章推荐
- UVALive 5971 Permutation Counting 组合学、递推
- UVAlive 5971 Permutation Counting
- UVAlive 5971 Permutation Counting(数论 dp)
- UVALive 6173 Casting
- UVALive - 3942 Remember the Word[Trie DP]
- UVALive 5811 Cards
- UVALive 3971 Assemble(二分 + 贪心)
- UVALive - 2689 Cricket Field
- UVALive6859 Points +凸包
- UVAlive 2326 Moving Tables(贪心 + 区间问题)
- UVALive 6933 Virus synthesis(回文树)
- UVaLive 5031 Graph and Queries (Treap)
- uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
- uvalive 2218
- UVALive 6926 Maximum Score(组合数学)
- UVALive 7043 International Collegiate Routing Contest(字典树)
- UVAlive 2326 Moving Tables(贪心 + 区间问题)
- UVALive-6485-Electric Car Rally(BFS)
- UVALive 6187 Never Wait for Weights 带权并查集
- 【 UVALive - 2197】Paint the Roads(上下界费用流)