# bzoj4517排列计数 错排+组合

#### 4517: [Sdoi2016]排列计数

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

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f
#define ll long long
#define N 1000005
#define mod 1000000007
using namespace std;
ll f[N],fac[N];

ll qp(ll a,int b){
ll c=1;
while(b){
if(b&1)c=(c*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return c;
}

int main(){
#ifdef wsy
freopen("data.in","r",stdin);
#else
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
#endif
int T;
scanf("%d",&T);
f[1]=0;f[2]=1;f[0]=1;
for(register int i=3;i<=1e6;i++)
f[i]=(i-1)*(f[i-1]+f[i-2])%mod;
fac[0]=fac[1]=1;
for(register int i=2;i<=1e6;i++)
fac[i]=(fac[i-1]*i)%mod;
while(T--){
int n,m;
scanf("%d%d",&n,&m);
ll t1=qp(fac[m],mod-2),t2=qp(fac[n-m],mod-2);
ll c=(fac[n]*t1)%mod;
c=(c*t2)%mod;
ll res=(f[n-m]*c)%mod;
cout<<res<<endl;
}
return 0;
}

If you live in the echo,
your heart never beats as loud.

posted @ 2017-12-03 19:43  _wsy  阅读(201)  评论(0编辑  收藏  举报