牛客组合数问题

题目链接

这个题略毒瘤,很简单的组合数(帕斯卡公式)+毒瘤前缀和

众所周知,使用帕斯卡公式可以预处理出组合数,而且是类似杨辉三角。
这题中的前缀和是二维前缀和,求8 3时,答案是红色区域而不是整块减去绿色区域。主要还是审题不够仔细吧。

另外处理这种三角前缀和时,需要特判边界的情况。

在这里插入图片描述

AC代码:

/*
 * @Author: hesorchen
 * @Date: 2020-04-14 10:33:26
 * @LastEditTime: 2020-06-25 21:31:07
 * @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 3123456777
#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 C[2010][2010];
ll ans[2010][2010];
ll t, m, k = 2;
void f_c()
{
    for (int i = 0; i <= 2000; i++)
        for (int j = 0; j <= i; j++)
        {
            if (!j)
                C[i][j] = 1;
            else
                C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % k;
            if (C[i][j] == 0)
                ans[i][j] = 1;
        }
}
void f_ans()
{
    for (int i = 0; i <= 2000; i++)
        for (int j = 0; j <= i; j++)
        {
            if (!i && !j)
                continue;
            else if (!i || i == j)
                ans[i][j] = ans[i][j - 1];
            else if (!j)
                ans[i][j] = ans[i - 1][j];
            else
                ans[i][j] = ans[i - 1][j] + ans[i][j - 1] - ans[i - 1][j - 1] + ans[i][j];
        }
}
int main()
{
    cin >> t >> k;
    f_c();
    f_ans();
    while (t--)
    {
        ll a, b;
        cin >> a >> b;
        b = min(a, b);
        cout << ans[a][b] << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hesorchen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值