HDU 5637 Transform
题意: 有两种变换:
1. 改变此数二进制的某一位(1变成0 或者 0变成1)
2. 让它与给出的n个数当中的任意一个做异或运算
给你两个数s, t,求从s到t最少要经过几步变换,一共m组查询
思路: 仔细观察会发现其实只与(s^t)有关,那么设x = s^t,那么x就是s和t二进制之间的差别(仔细想想就是s和t相同的位都是0, 不同的都是1,说明只要需要发生变化的位都是1),那么只需要用s^x就得到t了,所以只需要求最少变成x需要多少步,然后再加1就是答案了,那么答案就是从0变成x需要多少步。bfs一下即可。不过还要注意第一个变化条件,在bfs的写出来。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define pii pair<int, int>
using namespace std; const int maxn = 1e6;
const long long mod = 1e9 + ;
int res[maxn];
int a[];
void bfs(int n)
{
memset(res, -, sizeof(res));
res[] = ;
queue<pii> Q;
pii cur, nex;
cur.first = cur.second = ;
Q.push(cur);
int x = ( << );
while (!Q.empty())
{
cur = Q.front(); Q.pop();
for (int i = ; i < n; i++)
{
nex.first = cur.first ^ a[i];
nex.second = cur.second + ;
if (nex.first < x && res[nex.first] == -)
{
res[nex.first] = nex.second;
Q.push(nex);
}
}
for (int j = ; j < ; j++)
{
nex.first = cur.first ^ ( << j);
nex.second = cur.second + ;
if (nex.first < x && res[nex.first] == -)
{
res[nex.first] = nex.second;
Q.push(nex);
}
}
}
}
int main()
{
int T, n, m, s, t;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++) scanf("%d", &a[i]);
bfs(n);
long long ans = ;
for (int q = ; q <= m; q++)
{
scanf("%d%d", &s, &t);
t = s ^ t;
ans = (ans + (long long)q * res[t]) % mod;
}
printf("%d\n", (int)ans);
} return ;
}
HDU 5637 Transform的更多相关文章
- 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 5637 Transform 最短路
题目链接 异或的性质. 求s到t的最少步骤, 等价于求0到s^t的最少步骤. 通过最少的步骤达到s^t的状态, 等价于求0到s^t的最短路. 先将最短路求出来然后O(1)查询. #include &l ...
- hdu 5637 BestCoder Round #74 (div.2)
Transform Accepts: 7 Submissions: 49 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072 ...
- HDU 4644 BWT(Burrows–Wheeler transform+KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4644 题意:给出一个串,按照下面的步骤得到一个新串: (1)首先将其后面增加一个美元符号: (2)将每 ...
- HDU 5842 Lweb and String(Lweb与字符串)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 1217 (Floyd变形)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1217 Arbitrage (Floyd)
Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...
- HDU 5842 Lweb and String (水题)
Lweb and String 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...
随机推荐
- CSS六大选择器(注释css表里不能加注释!!)
@charset "utf-8"; /* CSS Document */ <!--标签控制器--> body{ background-color:#F00} <! ...
- 日常工作中使用的一些Mongodb语句
.通过_userID字段查询重复录入内容 > db.template.aggregate({}}},{$}}) .查询 db.template.find({"group_7ee1247 ...
- win8下光驱消失
导入这个注册表后重启,总算能读了..reg add "HKLM\System\CurrentControlSet\Services\atapi\Controller0" /f /v ...
- linux下建立无线wifi------简单实用!
一 安装必要软件安装hostapd : sudo apt-get install hostapd安装DHCP: sudo apt-get install dhcp3-server 二 配置 ...
- linux 以root用户登录ftp
ftp默认禁止以root用户登录. 可以修改 /etc/ftpusers 文件,把root注释掉,即可以root用户登录ftp
- Android学习笔记(十一)BroadcastReceiver动态注册、注销示例
在上一篇博文中简单介绍了一下BroadcastReceiver的相关知识点,本篇举一个在代码中动态的注册.注销BroadcastReceiver的栗子. 1.首先创建一个MyReceiver并继承Br ...
- 哈希(3) java中的hashcode
看看jdk api中对object类的描述 1.http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html 2首先你要知道每个对象都有 ...
- JavaScript高级程序设计57.pdf
表单序列化 首先了解一下浏览器如何将数据发送给服务器 对表单字段的名称和值进行URL编码,使用和号(&)分隔 不发送禁用的表单字段 只发送勾选的复选框和单选按钮 不发送type为“reset” ...
- [LeetCode] 73. Set Matrix Zeroes 解题思路
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow ...
- head tail 命令
[一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1000 [二]显示1000行到3000行 cat ...