题意: 有两种变换:

  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的更多相关文章

  1. HDU 5637 Transform 单源最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5637 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  2. HDU 5637 Transform 搜索

    题意:bc round 74 div1 1002 中文题 分析(官方题解):注意到答案实际上只和s⊕t有关, bfs预处理下从0到xx的最短步数, 然后查询O(1)回答即可. #include < ...

  3. hdu 5637 Transform 最短路

    题目链接 异或的性质. 求s到t的最少步骤, 等价于求0到s^t的最少步骤. 通过最少的步骤达到s^t的状态, 等价于求0到s^t的最短路. 先将最短路求出来然后O(1)查询. #include &l ...

  4. hdu 5637 BestCoder Round #74 (div.2)

    Transform  Accepts: 7  Submissions: 49  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: 131072 ...

  5. HDU 4644 BWT(Burrows–Wheeler transform+KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4644 题意:给出一个串,按照下面的步骤得到一个新串: (1)首先将其后面增加一个美元符号: (2)将每 ...

  6. HDU 5842 Lweb and String(Lweb与字符串)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. hdu 1217 (Floyd变形)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)   ...

  8. HDU 1217 Arbitrage (Floyd)

    Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...

  9. HDU 5842 Lweb and String (水题)

    Lweb and String 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...

随机推荐

  1. ANDROID_MARS学习笔记_S04_001_OAuth简介

    一.OAuth简介

  2. Linux搭建SVN 服务器

    Linux搭建SVN 服务器 1          安装SVN 2          使用客户端连接 2.1       使用windows的客户端 2.2       使用Linux下的命令行 3  ...

  3. 利用CMake生成动态或静态链接库工程

    install解释: TARGETS版本的install命令 install(TARGETS targets... [EXPORT <export-name>] [[ARCHIVE|LIB ...

  4. 转:Java图形化界面设计——布局管理器之FlowLayout(流式布局)其他请参考转载出处网址

    http://blog.csdn.net/liujun13579/article/details/7771191 前文讲解了JFrame.JPanel,其中已经涉及到了空布局的使用.Java虽然可以以 ...

  5. linux使用crontab -e 遇到No space left on device

    今天用linux的crontab -e编辑定时脚本的时候.遇到No space left on device的错误. 网上找了半天终于知道原因了,昨天晚上我的一个任务因为路径没写对,到时crontab ...

  6. NET Framework GAC目录构造

    转:http://www.yl1001.com/userzone.htm?doaction=article&art_id=5851381388387201 我们一般都知道,.NET Frame ...

  7. 计数方法(扫描线):JLOI 2016 圆的异或并

    Description 在平面直角坐标系中给定N个圆.已知这些圆两两没有交点,即两圆的关系只存在相离和包含.求这些圆的异或面 积并.异或面积并为:当一片区域在奇数个圆内则计算其面积,当一片区域在偶数个 ...

  8. HTML5与CSS3权威指南.pdf2

    第三章 HTML5的结构 article元素更强调独立性,section元素强调分段,div元素强调css的套用,aretcle元素和section元素在核实的情况下可以调换 nav元素用作页面导航的 ...

  9. JavaScript高级程序设计28.pdf

    classList属性 在操作类名时需要通过className属性添加.删除和替换类名 <div class="bd user disabled">...</di ...

  10. 【解决】Internet访问看似正常(无叹号受限)却打不开网页

    嘛╮(╯▽╰)╭ 可能是前几天中了一等奖败了人品 .. 今天果断受点小挫折 事情是这样的:昨晚电脑在不插电的情况下打了一小时“剑灵”,有点烫,电量剩30%,关机睡觉,今早发现上不去网页了! 桌面右下角 ...