2018 Multi-University Training Contest 8 1001 Character Encoding【容斥】

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=809

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define  LL long long

const LL N = 3e5 + 10;
const LL MAXN = 3e5;
const LL mod = 998244353;

LL fac[N];
LL inv[N];

LL qkm(LL base,LL mi)
{
LL ans=1;
while(mi){
if(mi&1) ans=ans*base%mod;
base=base*base%mod;
mi>>=1;
}
return ans;
}

LL C(LL n,LL m)
{
if(m>n) return 0;
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}

int main()
{

fac[0]=fac[1]=1;

for(LL i=2;i<=MAXN;i++)fac[i]=fac[i-1]*i%mod;

inv[MAXN]=qkm(fac[MAXN],mod-2);

for(LL i=MAXN-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;
LL T;
cin>>T;
while(T--){
LL n,m,k;
cin>>n>>m>>k;
LL ans=C(k+m-1,m-1);
LL p=1;
LL di = k+m-1-n;
LL num=1;
while(di>=m-1){
if(p){
ans=ans-C(m,num)*C(di,m-1)%mod+mod;
ans%=mod;
}else{
ans=ans+C(m,num)*C(di,m-1)%mod;
ans%=mod;
}
p=p^1;
num++;
di-=n;
}
cout<<ans<<endl;
}
return 0;
}