description
Find out how many sequences A of length n satisfy the following conditions:
(1) The n numbers of 1 ~ n appear once in each sequence.
(2) If the value of number I A[i] is i, then I is stable. Sequences that happen to have m numbers are stable
There may be many sequences satisfying the conditions. The number of sequences is modular to 10 ^ 9 + 7.
analysis
Firstly, there are stable but uncertain orders in (n) so there are (C ^{m} {n}_______________ schemes.
The remaining number of (n-m\) must not be placed in the position of their numerical values, so it is the number of (n-m\) misaligned schemes.
Let(f[i] denote the number of schemes that are misaligned I n(i) number, and now insert a number(n\) which has been misaligned in front of(n-1)number.
\ (n) must not be placed in the (n) position, but in the other (n-1)position.
If (n) is inserted into a (k) position and (k) is placed in (n), then the remaining (n-2)numbers are still staggered.
If \ (n \) is inserted in the \ (k \) position and \ (k \) is not placed in the \ (n \) position, there are \ (n-1 \) numbers in addition to \ (n \) in error.
Since (k) has the possibility of (n-1), then (f[i]=(i-1)*(f[i-1]+f[i-2])
The answer is (C^{m} {n} * f [n-m])
code
#pragma GCC optimize("O3") #pragma G++ optimize("O3") #include<stdio.h> #include<string.h> #include<algorithm> #define MAX 1000000 #define mod 1000000007 #define ll long long #define reg register ll #define fo(i,a,b) for (reg i=a;i<=b;++i) #define fd(i,a,b) for (reg i=a;i>=b;--i) using namespace std; ll f[MAX+5],fac[MAX+5],inv[MAX+5]; ll n,m,T; inline ll read() { ll x=0,f=1;char ch=getchar(); while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();} while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar(); return x*f; } inline ll pow(ll x,ll y) { ll z=1; while (y) { if (y%2)z=z*x%mod; x=x*x,y>>=1; } return z; } inline ll C(ll m,ll n) { return fac[n]*inv[m]%mod*inv[n-m]%mod; } int main() { freopen("permutation.in","r",stdin); freopen("permutation.out","w",stdout); f[0]=1,f[1]=0,f[2]=1,fac[0]=1,inv[0]=inv[1]=1; fo(i,1,MAX)fac[i]=fac[i-1]*i%mod; fo(i,2,MAX)inv[i]=(mod-mod/i)*inv[mod%i]%mod; fo(i,2,MAX)inv[i]=inv[i-1]*inv[i]%mod; fo(i,3,MAX)f[i]=(i-1)*(f[i-1]+f[i-2])%mod; T=read(); while (T--) { n=read(),m=read(); printf("%lld\n",C(m,n)*f[n-m]%mod); } return 0; }