P5123 [USACO18DEC]Cowpatibility(容斥)

Luogu5123

计算[两组数中有相同的]=\(\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);
}

猜你喜欢

转载自www.cnblogs.com/lizehon/p/10464527.html