比赛 寒假集训5 评测结果 AAAWWAWWWW
题目名称 на меня 最终得分 40
用户昵称 赵飞羽 运行时间 1.572 s
代码语言 C++ 内存使用 3.87 MiB
提交时间 2026-03-01 11:20:28
显示代码纯文本
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 5010, C = 8010;

int fac[C], inv[C];
int n, p, a[N], b[N], ans;

int ksm(int x, int k) {
    int res = 1;
	while (k != 0) {
        if (k % 2 == 1) res = res * x % p;
        x = x * x % p;
        k /= 2;
    }
    return res;
}

void init() {
	fac[0] = 1;
	for (int i = 1; i < C; i++) fac[i] = fac[i-1] * i % p;
	inv[C-1] = ksm(fac[C-1], p - 2);
	for (int i = C - 1; i >= 1; i--) inv[i-1] = inv[i] * i % p;
	return;
}

int cal(int x, int y) {
	if (x < y) return 0;
	return fac[x] * inv[y] % p * inv[x-y] % p;
}

signed main() {
	freopen("BBQ.in", "r", stdin);
	freopen("BBQ.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> p;
	init();
	for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
	for (int i = 1; i <= n; i++) {
		for (int j = i + 1; j <= n; j++) {
			ans = (ans + cal(a[i] + b[i] + a[j] + b[j], a[i] + a[j])) % p;
		}
	}
	cout << ans % p << "\n";
	return 0;
}