思路:

预处理错排

然后C(n,m)*s[n-m-1]就是答案了

特判n-m-1<0

//By SiriusRen
#include <cstdio>
using namespace std;
#define int long long
const int mod=1000000007,N=1000050;
int cases,n,m,fac[N],s[N];
int pow(int x,int y){
    int res=1;
    while(y){
        if(y&1)res=res*x%mod;
        x=x*x%mod,y>>=1;
    }return res;
}
int C(int x,int y){return fac[x]*pow(fac[x-y],mod-2)%mod*pow(fac[y],mod-2)%mod;}
signed main(){
    scanf("%lld",&cases);fac[0]=s[1]=1;
    for(int i=1;i<N;i++)fac[i]=fac[i-1]*i%mod;
    for(int i=2;i<N;i++)s[i]=(s[i-1]+s[i-2])*i%mod;
    while(cases--){
        scanf("%lld%lld",&n,&m);
        printf("%lld\n",C(n,m)*s[n-m-1<0?1:n-m-1]%mod);
    }
}

 

相关文章:

  • 2021-11-14
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-01-30
  • 2021-06-07
  • 2022-01-02
猜你喜欢
  • 2021-06-07
  • 2021-05-23
  • 2021-07-04
  • 2021-11-04
  • 2021-05-29
  • 2021-11-15
  • 2021-07-17
相关资源
相似解决方案