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++
#include <iostream> #include <vector> #include <algorithm> using namespace std; ty ...
- upload-labs 1-21关通关记录
0x01: 检查源代码,发现JS前端验证,关闭JS即可连接,或者手动添加.php,或者上传1.jpg,再抓包修改为php 0X02: if (($_FILES['upload_file']['type ...
- 那些最全面的Windows10安装pytorch踩过的坑以及如何应用
那些最全面的Windows10安装pytorch踩过的坑以及如何应用 一.pytorch简介 2017年1月,由Facebook人工智能研究院(FAIR)基于Torch推出了PyTorch.它是一个基 ...
- Python基础语法2-数据类型
一,数字. 2. 字符串类型 3.列表 4.元组 5.集合 6.字典 7.数据类型转换: 8.序列操作
- 【MySQL】SELECT语句 - 查询数据
第4章 检索数据 文章目录 第4章 检索数据 1.SELECT语句 2.检索单个列 3.检索多个列 4.检索所有列 5.检索不同的行 6.限制结果 7.使用完全限定的表名 8.小结 简单记录 - My ...
- CTFHub - Web(五)
eval执行: 1.进入网页,显示源码, <?php if (isset($_REQUEST['cmd'])) { eval($_REQUEST["cmd"]); } els ...
- nmap的理解与利用(初级)
在命令窗口下输入命令等待,可以用回车来查看进度 nmap进行探测之前要把域名通过dns服务器解析为ip地址,我们也可以使用指定的dns服务器进行解析. nmap --dns-servers 主机地址 ...
- Spring Cloud Alibaba学习笔记
引自B站楠哥:https://space.bilibili.com/434617924 一.创建父工程 创建父工程hello-spring-cloud-alibaba Spring Cloud Ali ...
- 2021年1月15日【深度学习DeepLearning(python)实战班】
深度学习网络课程QQ群群号: 1057802989(加群备注:杨春娇邀请) 强化学习QQ交流群群号: 872395038(加群备注:杨春娇邀请)
- 关于springboot2.X使用外部tomcat服务器进行部署的操作详细步骤
1.修改pom.xml文件(4个地方) ①<packaging>war</packaging>将其中的jar该为war ②<dependency> <grou ...