您的位置:首页 > 其它

数学(错排):BZOJ 4517: [Sdoi2016]排列计数

2016-06-12 19:24 579 查看

4517: [Sdoi2016]排列计数

Time Limit: 60 Sec Memory Limit: 128 MB
Submit: 693 Solved: 434
[Submit][Status][Discuss]

Description

求有多少种长度为 n 的序列 A,满足以下条件:
1 ~ n 这 n 个数在序列中各出现了一次
若第 i 个数 A[i] 的值为 i,则称 i 是稳定的。序列恰好有 m 个数是稳定的
满足条件的序列可能很多,序列数对 10^9+7 取模。

Input

第一行一个数 T,表示有 T 组数据。
接下来 T 行,每行两个整数 n、m。
T=500000,n≤1000000,m≤1000000

Output

输出 T 行,每行一个数,表示求出的序列数

Sample Input

5

1 0

1 1

5 2

100 50

10000 5000

Sample Output

0

1

20

578028887

60695423
  
  错排还是很简单的……

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=1000010;
const long long mod=1000000007LL;
long long f[maxn],fac[maxn];
long long Inv(int x){
return x==1?1:(mod-mod/x)*Inv(mod%x)%mod;
}

int main(){
#ifndef ONLINE_JUDGE
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
#endif
fac[0]=1;f[0]=1;f[1]=0;
for(int i=1;i<=1000000;i++)fac[i]=fac[i-1]*i%mod;
for(int i=2;i<=1000000;i++)f[i]=(i-1)*(f[i-1]+f[i-2])%mod;

int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
printf("%d\n",f[n-m]*fac
%mod*Inv(fac[m])%mod*Inv(fac[n-m])%mod);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: