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. maven 无法导入ojdbc 的jar包 解决方法

    由于maven无法在线安装ojdbc包,所有先在我们需要手动导入. 准备环境: 1.系统需要配置好jdk以及maven环境. 2.ojdbc的jar包,记住jar的路径,我的路径是:E:\jdbc\o ...

  2. UnityToLaya小插件-找出空格并替换

    unity导出的文件中经常会出现带有空格的节点或者文件夹 而这些空格在本地开发测试过程中不会出现,当这些带有空格路径的文件需要放到网络上时,就出现问题了 所以这里写了一个简单的查找并清理空格的插件, ...

  3. [C#.NET 拾遗补漏]14:使用结构体实现共用体

    在 C 和 C# 编程语言中,结构体(Struct)是值类型数据结构,它使得一个单一变量可以存储多种类型的相关数据.在 C 语言中还有一种和结构体非常类似的语法,叫共用体(Union),有时也被直译为 ...

  4. LeetCode222 判断是否为完全二叉树并求节点个数

    给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...

  5. ctfhub技能树—sql注入—报错注入

    打开靶机 payload 1 Union select count(*),concat((查询语句),0x26,floor(rand(0)*2))x from information_schema.c ...

  6. 深入解析vue响应式原理

    摘要:本文主要通过结合vue官方文档及源码,对vue响应式原理进行深入分析. 1.定义 作为vue最独特的特性,响应式可以说是vue的灵魂了,表面上看就是数据发生变化后,对应的界面会重新渲染,那么响应 ...

  7. 日常分享:关于时间复杂度和空间复杂度的一些优化心得分享(C#)

    前言 今天分享一下日常工作中遇到的性能问题和解决方案,比较零碎,后续会持续更新(运行环境为.net core 3.1) 本次分享的案例都是由实际生产而来,经过简化后作为举例 Part 1(作为简单数据 ...

  8. linux中的虚拟环境工具

    1.虚拟环境工具的学习 python的虚拟环境,其实就是在机器上,方便的创建出多个解释器,每个解释器运行一个项目,互相之间不受影响 2.virtualenv工具,可以方便的创建,使用,删除也很方便 3 ...

  9. 理解js闭包9大使用场景

    1.返回值(最常用) //1.返回值 最常用的 function fn(){ var name="hello"; return function(){ return name; } ...

  10. 面对key数量多和区间查询低效问题:Hash索引趴窝,LSM树申请出场

    摘要:Hash索引有两个明显的限制:(1)当key的数量很多时,维护Hash索引会给内存带来很大的压力:(2)区间查询很低效.如何对这两个限制进行优化呢?这就轮到本文介绍的主角,LSM树,出场了. 我 ...