题目:
题解:
这个网站有毒......找到了排队领号的赶脚
这道题是插板法的经典应用
首先我们拿出m个小球,还剩下n-m个小球。这n-m个小球一共有n-m+1个空(左右两边也可以),把这m个小球插入到这n-m+1个空里就是答案,即
Cmn−m+1
这m个小球的编号取决于它插入的位置,所以和选哪个小球没关系
快速幂+Lucas+费马小定理
代码:
#include <cstdio>
#define LL long long
using namespace std;
LL n,m,Mod;
LL fast_pow(LL a,LL p)
{
a%=Mod;
LL ans=1;
for (;p;p>>=1,a=a*a%Mod)
if (p&1)
ans=ans*a%Mod;
return ans;
}
LL cf(LL sh,LL Mod)
{
return fast_pow(sh,Mod-2);
}
LL C(int m,int n)
{
if (n>m) return 0;
int i;LL xi=1,sh=1;
for (i=m-n+1;i<=m;i++) xi=i*xi%Mod;
for (i=1;i<=n;i++) sh=sh*i%Mod;
return xi*cf(sh,Mod)%Mod;
}
LL Lucas(int m,int n)
{
if (n>m) return 0;
LL ans=1;
for (;n;n/=Mod,m/=Mod)
ans=ans*C(m%Mod,n%Mod)%Mod;
return ans;
}
int main()
{
while (scanf("%lld%lld%lld",&n,&m,&Mod)!=EOF)
printf("%lld\n",Lucas(n-m+1,m)); //从n-m+1个空中选m个
}