标签:就是 while 阶乘 乘法 stream 组合 原理 固定 HERE
\(k*i+r\equiv0(mod\,p)\)
\(k*r^{-1}+i^{-1}\equiv0(mod\,p)\)
\(\therefore \lfloor\frac{p}{i}\rfloor*(p\,mod\,i)^{-1}+i^{-1}\equiv0(mod\,p)\)
\(\therefore i^{-1}\equiv-\lfloor\frac{p}{i}\rfloor*(p\,mod\,i)^{-1}(mod\,p)\)
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const int mod=1e9+7;
int T,n,m;
ll D[N],inv[N],preinv[N],pre[N];
ll C(int n,int m){return ((pre[n]*preinv[m])%mod*preinv[n-m])%mod;}
int main(){
D[2]=1;inv[1]=preinv[1]=preinv[0]=pre[1]=1;
for(register int i=3;i<=N-5;i++)D[i]=((i-1)*((D[i-1]+D[i-2])%mod))%mod;
for(register int i=2;i<=N-5;i+=1){
inv[i]=-mod/i*inv[mod%i]%mod+mod;
preinv[i]=(preinv[i-1]*inv[i])%mod;}
for(register int i=2;i<=N-5;i++)pre[i]=(pre[i-1]*i)%mod;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
if(n<m)printf("0\n");
else if(n==m)printf("1\n");
else printf("%lld\n",(C(n,m)*D[n-m])%mod);}
return 0;
}
标签:就是 while 阶乘 乘法 stream 组合 原理 固定 HERE
原文地址:https://www.cnblogs.com/ForwardFuture/p/9270929.html