题目链接:点击打开链接
附上代码,这个题目有一个坑的地方就是n=r=1的时候,我没有特判(请教了中南Xu Shu大佬了还是没有找到错误),结果wa了一天(qaq,附上本菜鸡的代码)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL p=1e9+7;
const int SIZE=1e6+100;
LL inv[SIZE];
LL quick_mod(LL a, LL b)
{
LL ans = 1;
a %= p;
while(b)
{
if(b & 1) ans = ans * a % p;
b >>= 1;
a = a * a % p;
}
return ans;
}
LL C(LL n, LL m)
{
if(m > n) return 0;
LL ans = 1;
for(int i=1; i<=m; i++)
{
LL a = (n + i - m) % p;
LL b = i % p;
ans = ans * (a * inv[b]%p) % p;
}
return ans;
}
LL Lucas(LL n, LL m)
{
if(m == 0) return 1;
return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}
int main()
{
int T,nofcase=1;
for(int i=1;i<SIZE;i++)
inv[i]=quick_mod(i,p-2);
scanf("%d", &T);
while(T--)
{
LL n,m,r;
scanf("%lld%lld%lld",&n,&m,&r);
LL di=Lucas(m,r);
LL ai=0;
LL tmp=r;
for(int i=2;i<=r;i++){
int sign=(r-i)&1;
int a = (r-i+1);
int b = i ;
tmp=tmp*(a*inv[b]%p)%p;
if(!sign) ai=(ai+i*tmp%p*quick_mod(i-1,n-1)%p)%p;
else ai=(ai-i*tmp%p*quick_mod(i-1,n-1)%p+p)%p;
}
LL answer=di*ai%p;
if(n==1&&r==1) printf("Case #%d: %lld\n",nofcase++,m); //这里其实可以不用特判,因为0^0=1,不过这是我踩过的坑,特意标记出来~qaq
else printf("Case #%d: %lld\n",nofcase++,answer);
}
return 0;
}
/**********************************************************************
Problem: 1994
User: Siryuanshao
Language: C++
Result: AC
Time:3724 ms
Memory:9836 kb
**********************************************************************/