题目描述
研究证明,有一个因素在两头奶牛能否作为朋友和谐共处这方面比其他任何因素都来得重要——她们是不是喜欢同一种口味的冰激凌!
Farmer John 的 NN 头奶牛(2\le N\le 5\times 10^42≤N≤5×104)各自列举了她们最喜欢的五种冰激凌口味的清单。为使这个清单更加精炼,每种可能的口味用一个不超过 10^6106 的正整数 \texttt{ID}ID 表示。如果两头奶牛的清单上有至少一种共同的冰激凌口味,那么她们可以和谐共处。
请求出不能和谐共处的奶牛的对数。
输入输出格式
输入格式:
输入的第一行包含 NN。以下 NN 行每行包含 55 个整数(各不相同),表示一头奶牛最喜欢的冰激凌口味。
输出格式:
输出不能和谐共处的奶牛的对数。
输入输出样例
输入样例#1: 复制
4 1 2 3 4 5 1 2 3 10 8 10 9 8 7 6 50 60 70 80 90
输出样例#1: 复制
4
说明
在这里,奶牛 44 不能和奶牛 11、22、33 中的任一头和谐共处,奶牛 11 和奶牛 33 也不能和谐共处。
题解1:首先能想到容斥的思路,总共的对数是n*(n-1)/2,然后减去融洽的就可以了,但是不会写....看了题解才发现可以用string来处理容斥的部分....(好骚),具体实现直接看代码就可以了.
#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/
string a[10];
int Sheryang(){
int n;scanf("%d",&n);
ll ans=1LL*n*(n-1)/2;
map<string,int>mp;
for(int i=1;i<=n;i++){
for(int j=0;j<5;j++){
cin>>a[j];
}
sort(a,a+5);
ll tmp=0;
for(int j=1;j<(1<<5);j++){
int cnt=0;string s;
for(int k=0;k<5;k++){
if(j&(1<<k)){
cnt++;
s+=a[k];
}
}
if(cnt&1) tmp+=mp[s]++;
else tmp-=mp[s]++;
}
ans-=tmp;
}
printf("%lld\n",ans);
return 0;
}
第二种思路:直接使用bitset进行暴力,mp[i]代表喜欢i这个元素的人的集合,然后直接bitset暴力就可以了。
#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/
map<int,bitset<50005> >mp;
int a[50005][6];
int Sheryang(){
int n=read;
for(int i=1;i<=n;i++){
for(int j=1;j<=5;j++){
a[i][j]=read;
mp[a[i][j]].set(i);
}
}
bitset<50005>tmp;
ll ans=0;
for(int i=1;i<=n;i++){
tmp.reset();
for(int j=1;j<=5;j++) tmp|=mp[a[i][j]];
ans+=n-tmp.count();
}
printf("%lld\n",ans/2);
return 0;
}