题目:https://www.acwing.com/problem/content/232/
#include<bits/stdc++.h> #define ll long long #define mod 1000000007 using namespace std; const int maxn = 1000005; ll dp[maxn],inv[maxn],fac[maxn],inv_fac[maxn]; void init() { inv[0]=inv[1]=inv_fac[0]=fac[0]=1; dp[1]=0;dp[2]=1; for(int i=2; i<maxn; i++) inv[i]=inv[mod%i]*(mod-mod/i)%mod; for(int i=1; i<maxn; i++) fac[i]=fac[i-1]*i%mod; for(int i=1; i<maxn; i++) inv_fac[i]=inv_fac[i-1]*inv[i]%mod; for(int i=3; i<maxn; i++) dp[i]=(i-1)*(dp[i-2]+dp[i-1])%mod; } ll C(int n,int m) { return fac[n]*inv_fac[m]%mod*inv_fac[n-m]%mod; } int main() { init(); ll n,m,t; scanf("%lld",&t); while(t--){ scanf("%lld%lld",&n,&m); if(n==m) printf("1 "); else printf("%lld ",(C(n,m)*dp[n-m])%mod); } }