Bruteforce approach for this problem is going through all possible pairs of cows and check if the cows share a common flavor. But it takes to compute the pairs, which brings TLE error.
Let's focus on the number of flavors each cow like, since 5 is much smaller than 50,000. If we think about the possible flavor sets for each cow, there are cases.
Using PIE, we can count the number of such that and share common flavor.
If refers to number of cows that share more or equal to flavors with cow ,
The problem can be solved by summing up the s.
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
long long N, a[5], r;
map <vector<long long>, long long> m;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for(long long i=1; i<=N; i++){
for(long long i=0; i<5; i++)
cin >> a[i];
sort(a, a+5);
for(long long i=1; i<(1<<5); i++){
vector <long long> v;
for(long long k=0; k<5; k++){
if(i & (1<<k)) v.push_back(a[k]);
}
r+=(v.size()%2 ? 1 : -1) *m[v];
m[v]++;
}
}
cout << N*(N-1)/2-r;
return 0;
}