首页 > 其他 > 详细

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

时间:2017-11-05 19:01:44      阅读:307      评论:0      收藏:0      [点我收藏+]

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\n",ans);
46     return 0;
47 }
View Code

 

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

原文:http://www.cnblogs.com/137shoebills/p/7788121.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!