FZU2282--错排公式

http://acm.fzu.edu.cn/problem.php?pid=2282

F[i]=(i-1)(F[i-2]+F[i-1])

 1 #include<cstdio>
 2 #include<iostream>
 3 #define LL long long 
 4 #define Mod 1000000007
 5 #define maxn 10010
 6 using namespace std;
 7 int n,m,k;
 8 LL C[maxn][120],F[maxn],ans,tot[maxn];
 9 int main(){
10     F[1]=0,F[2]=1,tot[1]=1,tot[2]=2,tot[0]=1;
11     for(int i=3;i<=10000;i++){
12         F[i]=(i-1)*((F[i-2]+F[i-1])%Mod);
13         F[i]%=Mod;
14         tot[i]=tot[i-1]*i;
15         tot[i]%=Mod;
16     //    cout<<i<<' '<<tot[i]<<endl;
17     }
18     C[0][0]=1;
19     for(int i=1;i<=10000;i++){
20         C[i][0]=1;
21         for(int j=1;j<=min(i,101);j++){
22             C[i][j]=(C[i-1][j]+C[i-1][j-1])%Mod;
23         //    cout<<C[i][j]<<' ';
24         }
25         //cout<<endl;
26     }
27     int T;
28     cin>>T;
29     while(T--){
30         cin>>n>>k;
31         ans=0;
32         for(int i=0;i<k;i++){
33             ans+=C[n][i]*F[n-i];
34             ans%=Mod;    
35         }
36         LL res=tot[n]-ans;
37         while(res<0) res+=Mod;
38         cout<<(res%Mod)<<endl;
39     }
40     return 0;
41 }
原文地址:https://www.cnblogs.com/poler/p/7286941.html