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. 树莓派-4WD智能小车操作小结

    树莓派-4WD智能小车操作小结 树莓派4B-4WD智能小车,双层结构,第一层结构为:小车扩展板(底层)+树莓派主板,通过铜柱隔离固定,小车扩展板相当于计算机的外设扩展板:上面一层为第二层,是三个舵机承 ...

  2. 【SpringBoot1.x】SpringBoot1.x 安全

    SpringBoot1.x 安全 文章源码 环境搭建 SpringSecurity 是针对 Spring 项目的安全框架,也是 SpringBoot 底层安全模块默认的技术选型.他可以实现强大的 we ...

  3. 【MySQL 高级】架构介绍

    MySQL高级 架构介绍 MySQL 简介 MySQL 安装 Docker 安装 参考链接 Linux 安装 参考链接 MySQL 配置文件 log-bin:二进制日志文件.用于主从复制.它记录了用户 ...

  4. python中re模块的使用(正则表达式)

    一.什么是正则表达式? 正则表达式,又称规则表达式,通常被用来检索.替换那些符合某个模式(规则)的文本. 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合, ...

  5. git 遇到 fatal: loose object xxxx (stored in .git/objects/cb/xxxx) is corrupt 问题

    我的git版本是2.3.x,用下面这个参考链接的方法也可以解决 参考blog

  6. Windows同一软件不同窗口如何快速切换

    windows快速切换应用的快捷键是Alt + Tab 这个快捷键可以在多个应用之间快速切换,但是软件多开时,而此时我只想在同一软件内的多个窗口切换,一切换好多个窗口扑面而来,我还要去用找并用鼠标点击 ...

  7. 中间件:ElasticSearch组件RestHighLevelClient用法详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.基础API简介 1.RestHighLevelClient RestHighLevelClient的API作为ElasticSearch备 ...

  8. Py-时间,随机,os,sys,jsonpickle序列化,shelve,xml模块

    内置模块 1.时间模块 第一:time.time()是时间戳 时间戳默认是 从1970年到现在过的秒数,是一个很长的数值它可以做时间的计算以及显示 第二:localtime() 获取当前的时间,按元组 ...

  9. windows2012-2016亲测安装mysql8.0

    先去官网下载点击的MySQL的下载 下载完成后解压 解压完是这个样子 不要手动创建Data文件夹和my.ini文件, cmd命令窗口进入bin目录,如果已经做了环境变量那随意在哪里打开. mysqld ...

  10. mybatis框架整合及逆向工程

    mybatis框架整合及逆向工程 一.三大框架整合 ​ 整合SSM框架 1.导入pom文件 1.导入spring的pom依赖 <?xml version="1.0" enco ...