# [BZOJ4517][SDOI2016]排列计数(错位排列)

## 4517: [Sdoi2016]排列计数

Time Limit: 60 Sec  Memory Limit: 128 MB
Submit: 1616  Solved: 985
[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

0
1
20
578028887
60695423

## Source

[Submit][Status][Discuss]

$$ans=C_n^m*f_{n-m} \quad f_n=(n-1)(f_{n-1}+f_{n-2})$$

 1 #include<cstdio>
2 #include<algorithm>
3 #define rep(i,l,r) for (ll i=(l); i<=(r); i++)
4 typedef long long ll;
5 using namespace std;
6
7 const ll N=1000000,mod=1000000007;
8 ll n,m,T,fin[N+10],fac[N+10],f[N+10];
9
10 ll pow(ll a,ll b){
11     ll res;
12     for (res=1; b; a=(a*a)%mod,b>>=1)
13         if (b & 1) res=(res*a)%mod;
14     return res;
15 }
16
17 int main(){
18     freopen("permutation.in","r",stdin);
19     freopen("permutation.out","w",stdout);
20     fac[0]=1; rep(i,1,N) fac[i]=(fac[i-1]*i)%mod;
21     fin[N]=pow(fac[N],mod-2);
22     for (ll i=N-1; ~i; i--) fin[i]=(fin[i+1]*(i+1))%mod;
23     f[0]=1; f[1]=0; f[2]=1; rep(i,3,N) f[i]=((i-1)*(f[i-1]+f[i-2]))%mod;
24     for (scanf("%lld",&T); T--; )
25         scanf("%lld%lld",&n,&m),printf("%lld\n",((fac[n]*fin[m]%mod*fin[n-m])%mod*f[n-m])%mod);
26     return 0;
27 }

posted @ 2018-03-28 17:45  HocRiser  阅读(189)  评论(0编辑  收藏  举报