luogu1313计算系数题解--二项式定理
题目链接
https://www.luogu.org/problemnew/show/P1313
分析
二项式定理
\((a+b)^n=\sum_{k=0}^{n}{C^k_n a^k b^{n-k} }\)
于是我们要求的即是\(C^k_n \times a^n \times b^m\),于是直接快速幂,然后按公式\(C^k_n=\frac {n!}{(n-k)! \times k!}\),化成\(\prod_{i=k+1}^{i<=n} i \times ((n-k)!)^{-1}\)
代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <algorithm>
#include <cctype>
#define ll long long
#define ri register int
using std::min;
using std::max;
template <class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;return ;
}
const ll p=10007;
int n,m,k,a,b;
inline ll ksm(ll a,ll c){
ll ans=1;
while(c){
if(c&1)ans=ans*a%p;
a=a*a%p;
c=c>>1;
}
return ans;
}
ll fermat(ll a){
return ksm(a,p-2);
}
int main(){
/*ans=a^n*b^m*C(n,k)*/
ll ans=1;
read(a),read(b),read(k),read(n),read(m);
ans=ksm(a,n)*ksm(b,m)%p;
for(ri i=k;i>n;i--)ans=ans*i%p;
ll tmp=1;
for(ri i=k-n;i>=2;i--)tmp=tmp*i%p;
ans=ans*fermat(tmp)%p;
printf("%lld\n",ans);
return 0;
}