It turns out there is one factor that matters far more than any other when determining whether two cows are compatible as potential friends: whether they like similar flavors of ice cream!
Farmer John‘s N cows (2≤N≤50,000) have each listed their five favorite flavors of ice cream. To make this list concise, each possible flavor is represented by a positive integer ID at most 106. Two cows are compatible if their lists contain at least one common flavor of ice cream.
Please determine the number of pairs of cows that are NOT compatible
The first line of input contains N. Each of the following N lines contain 5 integers (all different) representing the favorite ice cream flavors of one cow.
Please output the number of pairs of cows that are not compatible.
Here, cow 4 is not compatible with any of cows 1, 2, or 3, and cows 1 and 3 are also not compatible.
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 1e5;
int op[] = {-1, 1, -1, 1, -1, 1};
struct node {
int n;
int v[6];
} e[maxn];
bool operator<(const node &a, const node &b) {
for (int j = 0; j < 5; j++) {
if (a.v[j] < b.v[j]) return true;
if (a.v[j] > b.v[j]) return false;
}
return false;
}
map<node, int> mp;
node ss(node &te, int j) {
node ret = {0, {0, 0, 0, 0, 0, 0}};
for (int i = 0; i < 5; i++) {
if ((1 << i) & j) ret.v[ret.n++] = te.v[i];
}
return ret;
}
int main() {
freopen("input.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
e[i].n = 5;
for (int j = 0; j < 5; j++) {
scanf("%d", &e[i].v[j]);
}
sort(e[i].v, e[i].v + 5);
for (int j = 1; j < 32; j++) {
mp[ss(e[i], j)]++;
}
}
ll ans = 1ll * n * (n - 1) / 2;//运算时转为longlong,否则会溢出
for (auto &p :mp) {
ans -= 1ll * op[p.first.n] * p.second * (p.second - 1) / 2;
}
printf("%lld\n", ans);
return 0;
}