计算[两组数中有相同的]=\(\sum_{i}\)两组数中\(i\)个数相同的组合方案
map <string,int> 操作\(:\)加上\(,\)防止算重
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<tr1/unordered_map>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define Debug(x) cout<<#x<<"="<<x<<endl
using namespace std;
typedef long long LL;
const int INF=1e9+7;
inline LL read(){
register LL x=0,f=1;register char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<3)+(x<<1)+(c&15),c=getchar();
return f*x;
}
tr1::unordered_map <string,LL> F;
string a[7];
LL n,ans,Ans;
int main(){
n=read();
Ans=n*(n-1)/2;
while(n--){
for(int i=1;i<=5;i++) cin>>a[i];
sort(a+1,a+6);//一般都要排序
ans=0;
for(int i=1;i<32;i++){
int cnt=0;string s="";
for(int j=1;j<=5;j++){
if(i&(1<<(j-1))) cnt++,s+=a[j]+",";//用,隔开防止算重
}
if(cnt&1) ans+=F[s]++;
else ans-=F[s]++;
}
Ans-=ans;//减去和谐的就是不和谐的
}
printf("%lld\n",Ans);
}
本文介绍了解决Luogu5123问题的算法思路,使用C++实现,通过map记录字符串组合,计算两组数中相同元素的组合方案,避免重复计算。文章展示了完整的代码实现,并解释了如何通过位运算和字符串拼接来统计不和谐的组合。
509

被折叠的 条评论
为什么被折叠?



