牛客組合數問題

題目鏈接

這個題略毒瘤,很簡單的組合數(帕斯卡公式)+毒瘤前綴和

衆所周知,使用帕斯卡公式可以預處理出組合數,而且是類似楊輝三角。
這題中的前綴和是二維前綴和,求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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章