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

时间: 2024-08-29 15:29:12

JZYZOJ1376 [coci2011]友好数对 容斥定理 状态压缩的相关文章

[coci2011]友好数对 容斥

无趣的小x在玩一个很无趣的数字游戏.他要在n个数字中找他喜欢友好数对.他对友好数对的定义是:如果有两个数中包含某一个以上相同的数位(单个数字),这两个数就是友好数对.比如:123和345 就是友好数对,因为都包含数位3,显然123和234也是由号数对.而12和34则不是友好数对,因为它们没有相同的数位. 刚拿到题没怎么读懂,因为我直观的想法是存一下扫一遍就行了,后来一想,得用容斥:又犯蠢了: 其实这道题的容斥比较基本,看代码吧: #include<iostream> #include<c

cf451E Devu and Flowers 卢卡斯定理+容斥定理

题目:http://codeforces.com/problemset/problem/451/E 题意:有n个盒子(n<=20),每个盒子中有10^12个小球,现从每个盒子中取出若干球(可为0),求共取出s个小球(s<=10^14)的方案数. 组合数学问题,求C(n,m).但n,m过大时,可用卢卡斯定理. 卢卡斯定理:C(n,m) %p = C(n/p,m/p) * C(n%p,m%p) 从n个盒子中取出s个球的方案数,相当于插板,即 C(s+n-1,n-1).注意这是没有限制条件的情况.

HDU1796 How many integers can you find【容斥定理】

题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包括零),求小于N的 正整数(1~N-1)中,能被M个整数的集合中随意一个元素整除的正整数个数. 比如N = 12.M = {2,3},在1~N-1中,能被2整除的数为{2,4,6.8.10},能被3整除的数为 {3.6,9}.则所求集合为{2,3,4.6,8,9,10},共7个,则答案为7. 思路:

题解报告:hdu 4135 Co-prime(容斥定理入门)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

容斥定理 hdu2204 Eddy&#39;s爱好

传送门:点击打开链接 很明显会有大量重复的被计算,所以很容易就想到容斥定理. 我们设dp[i]表示能表示成M^i(i>1)且i是这个数字能表示出来的最大的情况时的总类数 比如,27拆成M^K时的K最大能表示成3,所以27这个数字分在dp[3]这一类 1我们暂时不考虑,不把它放在任何一类 因为K>1,所以K至少是2,最大是2^K=N的时候,所以K最大等于log2(N),所以K非常的小 首先,求出K最大大概的位置 然后开始求dp[i].求法如下: 首先,1~N中有哪些是能拆分成M^i的,利用pow

HDU 1796 How many integers can you find (容斥定理 + 二进制)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5493    Accepted Submission(s): 1567 Problem Description Now you get a number N, and a M-integers set, you should

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

GCD SUM 强大的数论,容斥定理

GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   for(int j = 1; j <= M; j ++)       if(gcd(i,j)