比赛 2017noip 评测结果 AAAAAAAAAAAAAAAWAAAA
题目名称 组合数问题 最终得分 95
用户昵称 kZime 运行时间 0.394 s
代码语言 C++ 内存使用 31.40 MiB
提交时间 2017-09-20 20:51:39
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2016;
const int maxt = 10002;

int c[maxn][maxn], k, t, sum[maxn][maxn], a[maxt], b[maxt], ma, mb;

void init() {
    for (int i = 0; i <= ma; i++) c[i][i] = c[i][0] = 1;
    for (int i = 1; i <= ma; i++)
        for (int j = 1; j < i; j++)
            (c[i][j] = c[i - 1][j - 1] % k + c[i - 1][j] % k) %= k;
}

int main() { 
    freopen("problem.in", "r", stdin);
    freopen("problem.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin >> t >> k;
    for (int i = 1; i <= t; i++) {
        cin >> a[i] >> b[i];
        ma = max(a[i], ma);
        mb = max(b[i], mb);
    }
    init();
    for (int i = 0; i <= ma; i++) {
        for (int j = 0; j <= i; j++)
            sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + !(c[i][j]);
        for (int j = i + 1; j <= mb; j++)
            sum[i][j] = sum[i][j - 1];
    }
    for (int i = 1; i <= t; i++)
        cout << sum[a[i]][b[i]] << endl;
}
// 此致我的NOIP2016