Luogu P1313 计算系数
思路
在数据范围中已经给出了提示。即保证$N+M = K$
第一眼就知道要用二项式定理啊
二项式定理
先普及一下二项式定理,其实就是$(x+y)^n$的展开式,形式如下:
$$\large{(x+y)^n = {0\choose n}\times x^n+{{1}\choose {n-1}}\times x^{n-1}y+···+{{n-1}\choose n}\times xy^{n-1}{n\choose n}\times y^{n}}$$
继续分析思路
这题还有个不一样的地方就是,他并不是$x+y$而是$ax+by$。不过也没关系。$a$和$b$并不会影响。只是多了两步操作。
大家都知道幂运算遵循$(xy)^n = x^ny^n$,那么我们只需要将$a$和$b$的幂提出来就行了。
将$ax$和$by$看做两个整体。那么$(ax)^n$就变成了$a^nx^n$,$(by)^m$就变成了$b^my^m$。再结合二项式定理,那所要求的的系数就变成了$(a^n\times b^m)\times {n\choose k}$。
坑点
- 不要忘记取模
- 不要忘记开$longlong$
- $a$和$b$在输入的时候会很大所以先模一遍
- 要用快速幂哦
代码
#include <iostream> #include <cstdio> #include <cmath> #define LL long long const LL Mod = 10007; const LL maxn = 1003; using namespace std; LL c[maxn][maxn], a, b, k, n, m, Ans; inline LL Qpow(LL x, LL y) { LL ans = 1; LL base = x; while (y != 0) { if(y & 1) { ans = (ans%Mod) * (base%Mod); ans %= Mod; } base = (base%Mod) * (base%Mod); base %= Mod; y >>= 1; } return ans%Mod; } int main() { scanf("%lld%lld%lld%lld%lld", &a, &b, &k, &n, &m); a = a % Mod, b = b % Mod; Ans = Qpow(a, n)%Mod * Qpow(b, m)%Mod; Ans %= Mod; c[1][0] = 1, c[1][1] = 1; for(int i=2; i<=k; i++) for(int j=0; j<=i; j++) c[i][j] = c[i-1][j-1]%Mod+c[i-1][j]%Mod; Ans = c[k][n]%Mod * Ans%Mod; Ans %= Mod; printf("%lld", Ans); }
作者:Mystical-W
来源:http://www.cnblogs.com/bljfy
说明:客官~~您如果觉得写得好的话,记得给我点赞哦。如果要转载的请在合适的地方注明出处。谢
谢您的合作。您要是有什么有疑问的地方可以在下面给我评论。也可以直接私信我哦
声明:本作品由Mystical-W采用知识共享署名-非商业性使用-禁止演绎 4.0 国
际许可协议进行许可
来源:http://www.cnblogs.com/bljfy
说明:客官~~您如果觉得写得好的话,记得给我点赞哦。如果要转载的请在合适的地方注明出处。谢
谢您的合作。您要是有什么有疑问的地方可以在下面给我评论。也可以直接私信我哦
声明:本作品由Mystical-W采用知识共享署名-非商业性使用-禁止演绎 4.0 国
际许可协议进行许可