给定n,m,k,要求在n的全排列中,前m个数字中恰好有k个位置不变,有几种方案?
首先,前m个中k个不变,那就是C(m,k),然后利用容斥原理可得
ans=ΣC(m,k)*(-1)^i*C(m-k,i)*(n-k-i)! (0<=i<=m-k)
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#define ll long long
const ll Mod=;
int n,m,k;
ll jc[],jcny[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void exgcd(ll a,ll b,ll &x,ll &y){
if (b==){
x=;
y=;
return;
}
exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-(a/b)*y;
}
void init(){
jc[]=;jc[]=;jcny[]=;
for (int i=;i<=;i++)
jc[i]=(jc[i-]*i)%Mod;
ll x,y;
exgcd(jc[],Mod,x,y);
jcny[]=x%Mod;
for (int i=-;i>=;i--)
jcny[i]=(jcny[i+]*(i+))%Mod;
}
ll powit(ll x,ll y){
ll res=;
while (y){
if (y%) res=(res*x)%Mod;
x=(x*x)%Mod;
y/=;
}
return res;
}
ll C(int n,int m){
if (m==) return ;
return ((((jc[n]%Mod)*(jcny[n-m]%Mod))%Mod)*jcny[m])%Mod;
}
ll A(int x){
if (x==) return ;
return jc[x]%Mod;
}
int main(){
init();
int Tcase=;
int T=read();
while (T--){
printf("Case %d: ",++Tcase);
n=read(),m=read(),k=read();
ll ans=%Mod;
for (int i=;i<=m-k;i++)
ans=(ans+powit((ll)-,(ll)i)*A(n-k-i)*C(m-k,i)+Mod)%Mod;
ans=(ans*C(m,k))%Mod;
printf("%lld\n",ans);
}
}