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}$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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;
}