hdu5637 Transform (bfs+预处理)
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.
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.
where zi is
the answer for i-th
query.
3 3
1 2 3
3 4
1 2
3 9
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+预处理)的更多相关文章
- BZOJ-1189 紧急疏散evacuate BFS预处理+最大流+二分判定+神建模!!
绝世污题,垃圾题,浪费我一整天青春! 1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1262 ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)
Description In country Light Tower, a presidential election is going on. There are two candidates, ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- bzoj 1415(概率dp和bfs预处理)
感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec M ...
- hdu-5637 Transform(位运算+bfs)
题目链接: Transform Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)
快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚 ...
- HDU 3567 Eight II BFS预处理
题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...
随机推荐
- 【C++】《C++ Primer 》第九章
第九章 顺序容器 一.顺序容器概述 顺序容器(sequential container):为程序员提供了控制元素存储和访问顺序的能力.这种顺序不依赖于元素的值,而是与元素加入容器时的位置相对应. 不同 ...
- LeetCode198--打家劫舍问题
题目 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个 ...
- 二 prometheus 监控 Redis
Prometheus 监控Redis需要用到redis_exporter客户端, Prometheus -> redis_exporter这个模式, 类似监控Mysql 一个思路. 1 ) 设置 ...
- .NET 云原生架构师训练营(模块二 基础巩固 Scrum 核心)--学习笔记
2.7.2 Scrum 核心 3个工件 5个会议 5个价值观 3个工件 产品待办列表(Product Backlog) Sprint 待办列表(Sprint Backlog) 产品增量(Product ...
- 被集群节点负载不均所困扰?TKE 重磅推出全链路调度解决方案
引言 在 K8s 集群运营过程中,常常会被节点 CPU 和内存的高使用率所困扰,既影响了节点上 Pod 的稳定运行,也会增加节点故障的几率.为了应对集群节点高负载的问题,平衡各个节点之间的资源使用率, ...
- SpringBoot 好“吃”的启动原理
原创:西狩 编写日期 / 修订日期:2020-12-30 / 2020-12-30 版权声明:本文为博主原创文章,遵循 CC BY-SA-4.0 版权协议,转载请附上原文出处链接和本声明. 不正经的前 ...
- 【.NET 与树莓派】矩阵按键
欢迎收看火星卫视,本期节目咱们严重探讨一下矩阵按键. 所谓矩阵按键,就是一个小键盘(其实一块PCB板),上面有几个 Key(开关),你不按下去的时候,电路是断开的,你按下去电路就会接通.至于说有多少个 ...
- requests模块的基本使用
requests模块的基本使用 基于网络请求的模块. 环境的安装:pip install requests 作用:模拟浏览器发起请求 分析requests的编码流程: 1.指定url 2.发起了请求 ...
- VMware虚拟机提示“以独占方式锁定此配置文件失败”!!!
VMware异常关闭后再次启动提示"以独占方式锁定此配置文件失败"!!! 前几日在使用VMware虚拟机的时候,虚拟机突然出现了卡顿,然后就把电脑关机了重启,结果再次打开虚拟机的时 ...
- 如何实现new,call,apply,bind的底层原理。
new做了什么? new是用来实例化对象的,当new了一个对象后 1.创建一个新对象 2.将构造函数的作用域赋值给新对象(this指向新对象) 3.执行构造函数里面的代码(为这个新对象添加属性) 4. ...