JZYZOJ1376 [coci2011]友好数对 容斥定理 状态压缩

http://172.20.6.3/Problem_Show.asp?id=1376

题意:找给出的数中含有相同数字的数对的对数。

mmp数论题竟然卡快读,莫名拉低通过率什么的太过分了。

刚开始想到了怎么容斥但是没法实现,看了标程发现需要状压,我还是太菜了。

代码

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring>  
 4 #include<algorithm>  
 5 #include<cmath>
 6 using namespace std;
 7 const long long maxn=1<<11;
 8 const int modn=10000;
 9 long long n;
10 long long a[maxn+10]={};
11 long long b[maxn+10]={};
12 long long read(){
13     long long x=0;char ch=getchar();
14     while(ch>'9'||ch<'0')ch=getchar();
15     while(ch<='9'&&ch>='0'){x*=10;x+=ch-'0';ch=getchar();}
16     return x;
17 }
18 int main(){
19     n=read();
20     long long x,y=0;
21     int ma=1<<10;
22     for(int i=1;i<=n;i++){
23         x=read();;y=0;
24         while(x){
25             y|=1<<(x%10);x/=10;
26         }
27         a[y]++;
28     }
29     for(int i=1;i<ma;i++){
30         for(int j=i;j<ma;j++){
31             if((i|j)==j)b[i]+=a[j];
32         }
33     }
34     long long ans=0;
35     for(int i=1;i<ma;i++){
36         if(b[i]==0||b[i]==1)continue;
37         long long cnt=0;
38         for(int j=0;j<10;j++){
39             long long w=1<<j;w&=i;
40             if(w)cnt++;
41         }
42         if(cnt&1)ans+=b[i]*(b[i]-1)/2;
43         else ans-=b[i]*(b[i]-1)/2;
44     }
45     printf("%I64d
",ans);
46     return 0;
47 }
View Code
原文地址:https://www.cnblogs.com/137shoebills/p/7788121.html