zoukankan      html  css  js  c++  java
  • Luogu P1313 计算系数

    思路

    在数据范围中已经给出了提示。即保证$N+M = K$

    第一眼就知道要用二项式定理啊

    二项式定理 

    先普及一下二项式定理,其实就是$(x+y)^n$的展开式,形式如下:

    $$large{(x+y)^n = {0choose n} imes x^n+{{1}choose {n-1}} imes x^{n-1}y+···+{{n-1}choose n} imes xy^{n-1}{nchoose n} imes 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 imes b^m) imes {nchoose 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);
    }
  • 相关阅读:
    Eclipse 远程调试
    大数据处理方法bloom filter
    sicily 1259 Sum of Consecutive Primes
    sicily 1240. Faulty Odometer
    sicily 1152 简单马周游 深度优先搜索及回溯算法
    sicily 1050 深度优先搜索解题
    sicily 1024 邻接矩阵与深度优先搜索解题
    sicily 1156 二叉树的遍历 前序遍历,递归,集合操作
    sicily 1443 队列基本操作
    sicily 1006 team rankings 枚举解题
  • 原文地址:https://www.cnblogs.com/bljfy/p/9314570.html
Copyright © 2011-2022 走看看