牛客计算系数(Lucas+二项式定理)

二项式定理与算法应用
本文介绍了二项式定理的基本概念及其在算法题目中的应用,通过具体实例展示了如何利用二项式定理解决相关问题,并提供了一段AC代码作为参考。

题目链接
隐隐约约记得展开式和组合数的关系,但是记不起来,百度了才知道二项式定理。
( x + y ) n = C ( n , 0 ) × x n × y 0 + C ( n , 1 ) × x n − 1 × y 1 + C ( n , 2 ) × x n − 2 × y 2 . . . . . . + C ( n , n ) × x 0 × y n (x+y)^n=C(n,0)\times x^n \times y^0+C(n,1)\times x^{n-1} \times y^1+C(n,2)\times x^{n-2} \times y^2......+C(n,n)\times x^0 \times y^n (x+y)n=C(n,0)×xn×y0+C(n,1)×xn1×y1+C(n,2)×xn2×y2......+C(n,n)×x0×yn
知道二项式定理之后就是一个裸题了
答案就是 C ( k , n ) × a n × b m C(k,n)\times a^n\times b^m C(k,n)×an×bm

AC代码:

/*
 * @Author: hesorchen
 * @Date: 2020-04-14 10:33:26
 * @LastEditTime: 2020-06-30 22:12:29
 * @Link: https://hesorchen.github.io/
 */
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI acos(-1)
#define PB push_back
#define ll long long
#define INF 0x3f3f3f3f
#define mod 10007
#define pll pair<ll, ll>
#define lowbit(abcd) (abcd & (-abcd))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))

#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define FRE                              \
    {                                    \
        freopen("in.txt", "r", stdin);   \
        freopen("out.txt", "w", stdout); \
    }

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
//==============================================================================
ll quick_pow(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b /= 2;
    }
    return res % mod;
}
ll jc[2500];
void init()
{
    jc[0] = 1;
    for (int i = 1; i < 2345; i++)
        jc[i] = jc[i - 1] * i % mod;
}
ll C(ll n, ll m)
{
    ll a = n % mod;
    ll b = m % mod;
    if (a < b)
        return 0;
    if (a && b)
        return jc[a] * quick_pow(jc[b], mod - 2) % mod * quick_pow(jc[a - b], mod - 2) % mod * C(n / mod, m / mod) % mod;
    else
        return 1;
}
int main()
{
    ll a, b, k, n, m;
    cin >> a >> b >> k >> n >> m;
    init();
    cout << C(k, n) * quick_pow(a, n) * quick_pow(b, m) % mod << endl;
    return 0;
}
/*
*/
### 牛客 BM2 的 C++ 实现 BM2 是一道经典的算法题目,主要涉及二叉树的最大路径和问题。以下是基于已知信息以及常见解决思路给出的 C++ 实现。 #### 方法概述 该问题可以通过 **动态规划** 和 **递归回溯** 来解。核心思想是从根节点出发,计算经过任意节点的最大路径和。对于每一个节点,其贡献可以分为两种情况: 1. 节点作为路径的一部分(单边向下延伸)。 2. 节点成为路径的转折点(左子树 + 右子树 + 当前节点值)。 最终通过递归的方式自底向上更新全局最大值。 --- #### C++ 实现代码 ```cpp /** * Definition for a binary tree node. */ struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class Solution { public: int maxPathSum(TreeNode* root) { if (!root) return INT_MIN; // 边界条件处理 int globalMax = INT_MIN; helper(root, globalMax); return globalMax; } private: int helper(TreeNode* node, int& globalMax) { if (!node) return 0; // 左右子树的最大贡献值,忽略负数部分 int leftGain = std::max(helper(node->left, globalMax), 0); int rightGain = std::max(helper(node->right, globalMax), 0); // 更新当前节点为路径转折点时的最大值 int currentMax = node->val + leftGain + rightGain; globalMax = std::max(globalMax, currentMax); // 返回以当前节点为起点的一侧最大路径和 return node->val + std::max(leftGain, rightGain); } }; ``` --- #### 关键点解析 1. **递归函数设计** - `helper` 函数用于计算以某个节点为根的最大路径和,并将其结果存储到 `globalMax` 中。 - 对于每个节点,分别考虑左侧和右侧子树的最大贡献值[^3]。 2. **边界条件** - 如果当前节点为空,则返回 0 表示无贡献。 - 使用 `INT_MIN` 初始化全局变量 `globalMax`,确保能够正确记录最小可能值。 3. **优化细节** - 避免重复计算子树的最大路径和,利用递归特性一次完成所有节点的访问。 --- #### 时间与空间复杂度分析 - **时间复杂度**: O(n),其中 n 是二叉树中节点的数量。每个节点仅被访问一次。 - **空间复杂度**: O(h),h 是二叉树的高度。最坏情况下(退化成链表),空间复杂度为 O(n)[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值