# BZOJ 4517: [Sdoi2016]排列计数

## 4517: [Sdoi2016]排列计数

Time Limit: 60 Sec  Memory Limit: 128 MB
Submit: 911  Solved: 566
[Submit][Status][Discuss]

## Description

1 ~ n 这 n 个数在序列中各出现了一次

## Input

T=500000，n≤1000000，m≤1000000

5
1 0
1 1
5 2
100 50
10000 5000

## Sample Output

0
1
20
578028887
60695423

 1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<vector>
5 #include<cstdlib>
6 #include<cmath>
7 #include<cstring>
8 using namespace std;
9 #define maxn 1000010
10 #define llg long long
11 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
12 #define md 1000000007
13
14 llg T,n,m;
15 llg D[maxn],N[maxn];
16
17 void make_D()//错排递推式 f(n)=(n-1)*[f(n-1)+f(n-2)]
18 {
19     D[1]=0,D[2]=1;
20     for (llg i=3;i<=maxn-5;i++) D[i]=(i-1)*(D[i-1]+D[i-2]),D[i]%=md;
21 }
22
23 void maken()
24 {
25     N[1]=1;
26     for (llg i=2;i<=maxn-5;i++) N[i]=N[i-1]*i,N[i]%=md;
27 }
28
29 llg ksm(llg a,llg b,llg c)
30 {
31     if (b==0) return 1;
32     a%=md;
33     llg ans=1;
34     while (b!=0)
35     {
36         if (b%2) ans*=a,ans%=md;
37         b/=2;
38         a*=a, a%=md;
39     }
40     return ans;
41 }
42
43 int main()
44 {
45     cin>>T;
46     N[0]=D[0]=1;
47     maken(),make_D();
48     while (T--)
49     {
50         scanf("%lld%lld",&n,&m);
51         llg x=(N[n-m]*N[m]) % md;
52         llg ni=ksm(x,md-2,md);
53         printf("%lld\n",((N[n]*ni) % md)*D[n-m] % md);
54     }
55     return 0;
56 }

posted @ 2016-12-25 15:53  №〓→龙光←  阅读(164)  评论(0编辑  收藏  举报