solution.cpp 1.0 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <bits/stdc++.h>
using namespace std;
#define modk(x) (((x) >= k) ? ((x)-k) : (x))
const int maxn = 2005;
int c[maxn][maxn], n, m, k, T;
void init()
{
    ///预处理C(i,j)
    c[0][0] = 1;
    for (int i = 1; i < maxn; i++)
    {
        c[i][0] = 1 % k;
        for (int j = 1; j <= i; j++)
        {
            c[i][j] = modk(c[i - 1][j] + c[i - 1][j - 1]);
        }
    }
    ///处理C(i,j)是否为k 的倍数
    for (int i = 0; i < maxn; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            if (c[i][j] == 0)
                c[i][j] = 1;
            else
                c[i][j] = 0;
        }
    }
    ///将二维数组C处理成区域前缀和
    for (int i = 1; i < maxn; i++)
    {
        int s = 0;
        for (int j = 0; j < maxn; j++)
        {
            s += c[i][j];
            c[i][j] = c[i - 1][j] + s;
        }
    }
}
int main()
{
    scanf("%d%d", &T, &k);
    init();
    while (T--)
    {
        scanf("%d%d", &n, &m);
        printf("%d\n", c[n][m]);
    }
    return 0;
}