题目链接:

http://acm.fzu.edu.cn/problem.php?pid=2044

题意:

 给出 一大堆数,找出2个出现次数模3 为1 的两个数字  

题解:

把一个数分为几位拆开统计,统计完后,把所有的位数都模三,这样剩下的数就为a和b的叠加了,但是信息丢失太多了,没有办法把a,b区分开。

所以我们要多一个维护,在统计一个数对每一位的贡献的时候,同时统计任意两个位的联系的贡献(mp[i][j]++),这样最后把mp所有记录的联系都mod3剩下的就是a,b各自的联系了,这样就区分开了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int mp[][];
int cnt[],pos[],tot;
int main() {
int tc,n,x,a,b,i,j,k,t;
scanf("%d", &tc);
while (tc--) {
memset(cnt, , sizeof(cnt));
memset(mp, , sizeof(mp));
scanf("%d", &n);
for (i = ; i < n; i++) {
scanf("%d", &x);
tot = ;
for (j = ; j < ; j++) {
if (x&( << j)) {
pos[tot++] = j;
cnt[j]++;
}
}
for (j = ; j < tot; j++) {
for (k = j + ; k < tot; k++) {
mp[pos[j]][pos[k]]++;
mp[pos[k]][pos[j]]++;
}
}
}
for (i = ; i < ; i++) cnt[i] %= ;
for (i = ; i < ; i++) {
for (j = ; j < ; j++) {
mp[i][j] %= ;
}
}
a = ;
for (i = ; i < ; i++) {
if (cnt[i]) {
cnt[i]--;
a ^= ( << i);
for (j = ; j < ; j++) {
if (mp[i][j]) {
mp[i][j]--; mp[j][i]--;
cnt[j]--;
a ^= ( << j);
}
}
break;
}
}
b = ;
for (i = ; i < ; i++) {
if (cnt[i]) b ^= ( << i);
}
if (a > b) { t = a; a = b; b = t; }
printf("%d %d\n", a, b);
}
return ;
}

foj 2044 1 M possible 二进制压缩的更多相关文章

  1. HDU-1074.DoingHomework(撞鸭dp二进制压缩版)

    之前做过一道二进制压缩的题目,感觉也不是很难吧,但是由于见少识窄,这道题一看就知道是撞鸭dp,却总是无从下手....最后看了一眼博客,才顿悟,本次做这道题的作用知识让自己更多的认识二进制压缩,并无其它 ...

  2. poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析

    题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...

  3. poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推

    题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. Codeforces Round #371 (Div. 2) C. Sonya and Queries —— 二进制压缩

    题目链接:http://codeforces.com/contest/714/problem/C C. Sonya and Queries time limit per test 1 second m ...

  5. UVA690-Pipeline Scheduling(dfs+二进制压缩状态)

    Problem UVA690-Pipeline Scheduling Accept:142  Submit:1905 Time Limit: 3000 mSec  Problem Descriptio ...

  6. HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others)  ...

  7. Fliptile (dfs+二进制压缩)

    Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He ha ...

  8. POJ-3279.Fliptile(二进制状态压缩 + dfs) 子集生成

    昨天晚上12点刷到的这个题,一开始一位是BFS,但是一直没有思路.后来推了一下发现只需要依次枚举第一行的所有翻转状态然后再对每个情况的其它田地翻转进行暴力dfs就可以,但是由于二进制压缩学的不是很透, ...

  9. poj3254二进制放牛——状态压缩DP

    题目:http://poj.org/problem?id=3254 利用二进制压缩状态,每一个整数代表一行的01情况: 注意预处理出二进制表示下没有两个1相邻的数的方法,我的方法(不知为何)错了,看到 ...

随机推荐

  1. 请注意designer中的NAME field

    If you are trying to set the value of a field through PeopleCode and that field happens to be NAME t ...

  2. MapReduce数据流

    图4.5细节化的Hadoop MapReduce数据流 图4.5展示了流线水中的更多机制.虽然只有2个节点,但相同的流水线可以复制到跨越大量节点的系统上.下去的几个段落会详细讲述MapReduce程序 ...

  3. 给Activity设置背景颜色

    为了使得错误提示更加显眼,再用Toast+振动效果之外考虑变换整个activity的背景颜色. 尝试一: activity并没像winform一样直接给个属性来设置,就想获取整个activity的la ...

  4. about the pageload and page init event

    Page_Init The Page_Init event is the first to occur when an ASP.NET page is executed. This is where ...

  5. jquery Mobile应用第2课《构建跨平台APP:jQuery Mobile移动应用实战》连载二(简单的QWER键盘)

    在jQuery Mobile的布局中,控件大多都是单独占据页面中的一行,按钮自然也不例外,但是仍然有一些方法能够让多个按钮组成一行,比如说在范例6-5中就利用按钮分组的方法使4个按钮并列在一行中,如图 ...

  6. salt-ssh安装及简单使用

    需要 salt-master 0.17以上版本支持 1.安装 相关依赖包可查看requirements.txt Jinja2 M2Crypto msgpack-python pycrypto PyYA ...

  7. struts2传递参数值的3中方式

    在使用struts2的时候,当要传递的参数不多的时候,我们会选择使用属性来传参,而当要传递的参数很多的时候,或者多个action会有共用的参数时,我们会使用另外两种传参方式. 注意:使用Model D ...

  8. [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...

  9. Object-c 语法 - NSObject常用方法和反射

    NSObject常用方法 - (BOOL)isKindOfClass:(Class)aClass //判断是否为aClass或者aClass的子类的实例,aClass可以通过[类名 class]获取 ...

  10. mysql字段累加concat

    update tablename set field1=concat(field1,'_bak') where field2 like '%@xxx’