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 ...
随机推荐
- http://jinnianshilongnian.iteye.com/blog/2018936
http://jinnianshilongnian.iteye.com/blog/2018936
- [转贴] C++内存管理检测工具 Valgrind
用C/C++开发其中最令人头疼的一个问题就是内存管理,有时候为了查找一个内存泄漏或者一个内存访问越界,需要要花上好几天时间,如果有一款工具能够帮助我们做这件事情就好了,valgrind正好就是这样的一 ...
- SQLite 对中文路径的支持(用到了StringToWideChar和Utf8Encode在D7的System单元中自带)
最近用SQLITE作为数据库,发现,如果直接传递带中文路径或文件名的数据库,会导致无法打开数据库的情况.看了一下SQLITE的源码,才发现,原来SQLITE中是用UTF8编码进行文件打开操作的. 所以 ...
- Trie 树(转)
看了很多 Trie 树的介绍, 这篇讲的最好,简单易懂(特别是代码部分),直接转载:http://www.cnblogs.com/dolphin0520/archive/2011/10/11/2207 ...
- BZOJ1831: [AHOI2008]逆序对
1831: [AHOI2008]逆序对 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 341 Solved: 226[Submit][Status] ...
- Azure 媒体服务的 RTMP 支持和实时编码器
Cenk Dingiloglu Azure 媒体服务高级项目经理 直播流媒体目前已在公共预览版中提供,其中一项受支持的输入协议便是 RTMP.RTMP 是用于输入和分发包括直播流媒体在内的丰富媒体的一 ...
- Linux文件虚拟机系统只读Read-only file system的快速解决方法
问题描述:上周公司的私有云(底层架构是Openstack+KVM,目前稳定性还不够好,开发团队在改进中)一个计算节点挂掉,之后恢复后发现这个计算节点的所有Linux系统都变成只读了,复制文件提示:Re ...
- Modifying the ASP.NET Request Queue Limit
Modifying the ASP.NET Request Queue Limit When ASP.NET is queried, the request for service is carrie ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- Bzoj 1598: [Usaco2008 Mar]牛跑步 dijkstra,堆,K短路,A*
1598: [Usaco2008 Mar]牛跑步 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 427 Solved: 246[Submit][St ...