题目链接:

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. C#操作xml

    最进在做一个项目,需要用到xml做配置文件,但是自己忘的差不多了,所以就温习了一遍.以下是我项目中所用到的,所以肯定也不全面. 1.新建xml文件 using System.Xml;//需要引用 st ...

  2. Vue.js学习 Item1 --快速入门

    我们以 Vue 数据绑定的快速导览开始.如果你对高级概述更感兴趣,可查看这篇博文. 尝试 Vue.js 最简单的方法是使用 JSFiddle Hello World 例子.在浏览器新标签页中打开它,跟 ...

  3. tomcat学习笔记2

    LNMT在网站架构中的实现过程: Client --> http --> Nginx --> reverse_proxy (http) --> tomcat (http con ...

  4. python restful 框架之 eve 外网访问设置

    官网地址: http://python-eve.org/ 配合mongodb进行crud使用起来很方便,但是部署的时候遇到一个问题,按照官网和Deom说的,servername使用 '127.0.0. ...

  5. Python之Flask Web开发

    下载python包管理工具Pip: 访问网址:https://pip.pypa.io/en/stable/installing/    下载文件get-pip.py到本地计算机 定位到get-pip. ...

  6. STL之迭代器

    容器支持的迭代器类型  STL Container  Type of Iterators Supported   vector  random access iterators 元素严格有序(类似数组 ...

  7. python 中 input 和 raw_input 的区别

    input会假设输入的信息是合法的python表达式,例如,输入一个人名,Diesel,input会认为这是一个变量,必须加上引号,比如“Diesel”: 而raw_input会把所有的输入当作原始数 ...

  8. java基本概念

    什么是环境变量? 环境变量通常是指在操作系统当中,用来指定操作系统运行时需要的一些参数.通常为一系列的键值对. path环境变量的作用 path环境变量是操作系统外部命令搜索路径 什么是外部命令搜索路 ...

  9. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

  10. hdu 5058 So easy

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5058 So easy Description Small W gets two files. Ther ...