Educational Codeforces Round 115 (Rated for Div. 2) D. Training Session
https://codeforces.com/contest/1598/problem/D
题意
给定 \(n\) 道题,每题都有专题 \(a\) 和难度 \(b\), 保证给定的 \(n\) 道题中没有完全相同的,问选择3道不同的题目,使得三题的专题都不相同或者难度都不相同的选取方案数。
Tutorial
考虑容斥,首先总取法为 \(C^3_n = \cfrac{n*(n-1)*(n-2)}{6}\) ,其中可能存在三题专题和难度都有相同的方案 。注意到这样的方案一定有以下形式 \((a_1,b_1),(a_1,b_2),(a_2,b_1)\),因此可以选取 \((a_1,b_1)\) 作为中间对,还需要选择剩下两个,分别是和 \(a_1\) 相同专题 和 和 \(b_1\) 相同难度的,因此这时的答案是 \((cnt_{a_1} - 1)*(cnt_{b_1}-1)\)
点击查看代码
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
typedef long long ll;
#define endl '\n'
#define P pair<int, int>
#define eps 1e-8
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
const int N = 4e5 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n;
int main() {
IOS int T;
cin >> T;
while (T--) {
cin >> n;
vector<int> c1(n+1), c2(n+1),a(n),b(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
c1[a[i]]++;
c2[b[i]]++;
}
unsigned long long ans = n * (n - 1) * (n - 2) / 6;
for (int i = 0; i < n;i++) {
ans -= 1ll * (c1[a[i]]- 1) * (c2[b[i]] - 1);
}
cout << ans << endl;
}
}

浙公网安备 33010602011771号