hdu-5637 Transform(位运算+bfs)
题目链接:
Transform
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 685 Accepted Submission(s): 244
+ 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.
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 nintegers 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.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,l,r,a[],num[],flag[];
const int mod=1e9+;
queue<int>qu;
int bfs()
{
memset(flag,,sizeof(flag));
for(int i=;i<=2e5;i*=)
{
a[n++]=i;
}
qu.push();
num[]=;
flag[]=;
while(!qu.empty())
{
int top=qu.front();
qu.pop();
for(int i=;i<n;i++)
{
if(!flag[a[i]^top])
{
qu.push(a[i]^top);
num[a[i]^top]=num[top]+;
flag[a[i]^top]=;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
bfs();
ll ans=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&l,&r);
// cout<<num[l^r]<<" "<<i
ans+=(ll)(num[l^r]*i);
ans%=mod;
}
cout<<ans<<"\n"; } return ;
}
hdu-5637 Transform(位运算+bfs)的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- POj 1753--Flip Game(位运算+BFS)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30669 Accepted: 13345 Descr ...
- HDU 5637 Transform
题意: 有两种变换: 1. 改变此数二进制的某一位(1变成0 或者 0变成1) 2. 让它与给出的n个数当中的任意一个做异或运算 给你两个数s, t,求从s到t最少要经过几步变换,一共m组查询思路: ...
- HDU 5637 Transform 单源最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5637 题意: http://bestcoder.hdu.edu.cn/contests/contes ...
- HDU 5637 Transform 搜索
题意:bc round 74 div1 1002 中文题 分析(官方题解):注意到答案实际上只和s⊕t有关, bfs预处理下从0到xx的最短步数, 然后查询O(1)回答即可. #include < ...
- hdu 5014(贪+位运算)
题意:给你n+1个数(0->n),让你为这n+1个数在0->n中分别找一个数与其异或,求最后的最大值 思路:假设一个数5 (二进制1 0 1),则找的另一个数在5的0位上最好是1 , 1位 ...
- hdu 5637 Transform 最短路
题目链接 异或的性质. 求s到t的最少步骤, 等价于求0到s^t的最少步骤. 通过最少的步骤达到s^t的状态, 等价于求0到s^t的最短路. 先将最短路求出来然后O(1)查询. #include &l ...
- P1457 城堡 The Castle 位运算+BFS+思维(难题,好题)
题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张"幸运爱尔兰"(一种彩票).结果这张彩票让他获得了这次 ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
随机推荐
- java多线程实现复制大文件
有些开发的时候我们经常遇到这样一个问题,对大文件的处理.比如:日志文件.那么十几G的大文件.我们应该如何复制呢? 还有就是希望从本地和远程复制文件,文件都很大,10G级的如何办呢? 在这里我告诉你们, ...
- easyui combobox 三级级联 input 两种实现
/**<img src='${pageContext.request.contextPath}/images/block.png'/> * @param 默认载入 省市 */ $(func ...
- Mac下nginx安装和配置
nginx安装 brew search nginx brew install nginx 安装完以后,可以在终端输出的信息里看到一些配置路径: /usr/local/etc/nginx/nginx.c ...
- Django中的Cookie和Session操作以及CBV
1.Cookie 平常我们在浏览网页的时候,在需要输入密码的地方,如果已经登陆了一次,并且时间间隔比较近的话,是不需要登陆的,为什么了?这就是Cookie的作用. Cookie(或Cookies)指某 ...
- Linux:进程管理
Linux:进程管理 进程间通信 文件和记录锁定. 为避免两个进程间同时要求访问同一共享资源而引起访问和操作的混乱,在进程对共享资源进行访问前必须对其进行锁定,该进程访问完后再释放.这是UNIX为共享 ...
- 数据库抽象层PDO
通过数据库抽象层PDO可以访问多个数据库 //数据库抽象层PDO //造DSN:驱动名:dbname=数据库名:host=服务器地址 $dsn = "mysql:dbname=mydb;ho ...
- Mysql 导入实战
这个几天公司迁移预览版数据库,当前公司使用的是 Mysql 数据库,版本为 5.6.迁移的数据库大小也不算很大,2G 多一点,总体以小表为主,就几张表数据比较大,有业务记录表达到了 150W 的数量级 ...
- [原创] Windows下Eclipse连接hadoop
1 下载hadoop-eclipse-plugin :我用的是hadoop-eclipse-plugin1.2.1 ,百度自行下载 2 配置插件:将下载的插件解压,把插件放到..\eclipse\pl ...
- 【leetcode刷题笔记】Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 【Flask】query可用参数
### query可用参数:1. 模型对象.指定查找这个模型中所有的对象.2. 模型中的属性.可以指定只查找某个模型的其中几个属性.3. 聚合函数. * func.count:统计行的数量. * fu ...