Problem Description
A list of n integers
are given. For an integer x you
can do the following operations:

+ let the binary representation of x be b31b30...b0¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯,
you can flip one of the bits.

+ let y be
an integer in the list, you can change x to x⊕y,
where ⊕ means
bitwise exclusive or operation.

There are several integer pairs (S,T).
For each pair, you need to answer the minimum operations needed to change S to T.
 

Input
There are multiple test cases. The first line of input contains an integer T (T≤20),
indicating the number of test cases. For each test case:

The first line contains two integer n and m (1≤n≤15,1≤m≤105) --
the number of integers given and the number of queries. The next line contains n integers a1,a2,...,an (1≤ai≤105),
separated by a space.

In the next m lines,
each contains two integers si and ti (1≤si,ti≤105),
denoting a query.
 

Output
For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7),
where zi is
the answer for i-th
query.
 

Sample Input

1
3 3
1 2 3
3 4
1 2
3 9
 

Sample Output

10

题意:给你n个数,有m个询问,每一个询问有两个值a,b,每一次操作,你可以把a中的二进制的一位异或1,即那位从0变为1或者1变为0,或者你可以异或上n个数中的一个数,问最小变化的次数。一开始打算m个询问直接模拟,但是发现时间爆了,所以采用预处理的方案,然后每次询问花O(1)的时间。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000050
#define MOD 1000000007
int vis[1<<(20)],a[20],dis[1<<(20)];
int q[1111111][2];
int n;
int maxx; void bfs()
{
int i,j;
memset(vis,0,sizeof(vis));
int front,rear,x,y,xx,yy,state1;
front=rear=1;
q[rear][0]=0;q[rear][1]=0;
vis[0]=1;dis[0]=0;
while(front<=rear)
{
int state=q[front][0];
int num=q[front][1];
front++;
for(i=0;i<18;i++){
state1=(state^(1<<i));
if(vis[state1 ]==0 ){
dis[state1 ]=num+1;
vis[state1 ]=1;
if(state1>200050)continue;
rear++;
q[rear][0]=state1;
q[rear][1]=num+1;
}
}
for(i=1;i<=n;i++){
state1=(state^a[i]);
if(vis[state1 ]==0 ){
dis[state1 ]=num+1;
vis[state1 ]=1;
if(state1>200005)continue;
rear++;
q[rear][0]=state1;
q[rear][1]=num+1;
}
}
}
} int main()
{
int m,i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int maxx=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
bfs();
ll sum=0;
int c,d;
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
sum=(sum+(ll)i*(ll)dis[c^d] )%MOD;
}
printf("%lld\n",sum);
}
return 0;
}

hdu5637 Transform (bfs+预处理)的更多相关文章

  1. BZOJ-1189 紧急疏散evacuate BFS预处理+最大流+二分判定+神建模!!

    绝世污题,垃圾题,浪费我一整天青春! 1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1262 ...

  2. HDU - 1430 魔板 (bfs预处理 + 康托)

    对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...

  3. 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)

    Description In country Light Tower, a presidential election is going on. There are two candidates,   ...

  4. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

  5. bzoj 1415(概率dp和bfs预处理)

    感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  M ...

  6. hdu-5637 Transform(位运算+bfs)

    题目链接: Transform Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Other ...

  7. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  8. [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)

    快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚 ...

  9. HDU 3567 Eight II BFS预处理

    题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...

随机推荐

  1. Nginx配置请求头

    最近发现一个问题: IOS访问后台接口是,总是application/json;charset=utf-8 但是后台接口只支持大写的UTF-8,修改了Nginx的请求头之后正常. proxy_set_ ...

  2. 【SpringMVC】SpringMVC 拦截器

    SpringMVC 拦截器 文章源码 拦截器的作用 SpringMVC 的处理器拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 谈到拦截器,还有另外一个概 ...

  3. 【C++】《Effective C++》第四章

    第四章 设计与声明 条款18:让接口容易被正确使用,不易被误用 请记住 好的接口很容易被正确使用,不容易被误用.你应该在你的所有接口中努力达到这些性质. "促进正确使用"的办法包括 ...

  4. 2.jmeter组件介绍

    jmeter默认语言设置:  1.临时设置: 进入options -- Choose Language -- 选择中文简体,设置后语言  切换成中文,重启失效  2.永久设置:进入jmeter目录下的 ...

  5. 【ORA】ORA-39002,ORA-39070,ORA-29283, ORA-06512,ORA-29283解决办法

    今天使用IMPDP导入的时候报了一个错误 ORA-39002: invalid operation  ORA-39070: Unable to open the log file.  ORA-2928 ...

  6. 【Oracle】CBO优化详解

    SQL优化是数据优化的重要方面,本文将分析Oracle自身的CBO优化,即基于成本的优化方法.Oracle为了自动的优化sql语句需要各种统计数据作为优化基础.外面会通过sql的追踪来分析sql的执行 ...

  7. C# datagridview设置标题为汉语

    正常情况下,在给datagridview绑定数据源之后,显示的是SQL语句中的栏位,如下 我们想让标题显示汉语,可以有一下两种方法 1.在SQL中设置列别名 SELECT TITLE AS '报警标题 ...

  8. torch.optim.SGD()各参数的解释

    看pytorch中文文档摘抄的笔记. class torch.optim.SGD(params, lr=, momentum=0, dampening=0, weight_decay=0, neste ...

  9. YOLOv4

    @ 目录 YOLO v4源码 CMake安装 CUDA安装 cuDNN安装 OpenCV安装 Cmake编译 VS编译 图像测试 测试结果 YOLOv4是最近开源的一个又快又准确的目标检测器. 首先看 ...

  10. Cisco之show基础命令

    #show  version:显示版本信息等 #show running-config:显示当前(活动,并不一定保存)的配置 #show interfaces fastEthernet 0/1:进入接 ...