题意: 有两种变换:

  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. [topcoder]TopographicalImage

    BFS.这里用了queue,以及在数据结构里存了上一个的高度.也可以递归调用完成BFS,在调用之前做判断:http://community.topcoder.com/stat?c=problem_so ...

  2. 解决wordpress上传的文件尺寸超过 php.ini 中定义的 upload_max_filesize 值。

    上传的文件尺寸超过 php.ini 中定义的 upload_max_filesize 值. 解决方法:修改/etc/php5/apache2/php.ini文件中的 post_max_size = 6 ...

  3. MST(prim)+树形dp-hdu-4756-Install Air Conditioning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4756 题目意思: n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个 ...

  4. perl 对象

    唯一标识: 很明显,一个%employee 是不够的,每个雇员都要求有一个唯一标识和他或她自己的属性集合. 你可以动态的分配这个数据结构,也可以返回一个指向局部数据结构的引用 Vsftp:/root/ ...

  5. MySQL源码之两阶段提交

    在双1的情况下,两阶段提交的过程 环境准备:mysql 5.5.18, innodb 1.1 version配置: sync_binlog=1 innodb_flush_log_at_trx_comm ...

  6. Android Loader详解二:使用加载器

    一个使用装载器的应用会典型的包含如下组件: 一个Activity或Fragment. 一个LoaderManager的实例. 一个加载被ContentProvider所支持的数据的CursorLoad ...

  7. (转载)JavaScript中的Window窗口对象

    (转载)http://www.ijavascript.cn/jiaocheng/javascript-window-65.html 例子: <html> <head> < ...

  8. 很反感Java Web 三层框架

    为什么反感,大家 看图就知道了.一层一层的框架叠加,要达到精通就要一个一个框架的去深入了解,谁有这个精力?PHP没有这么多框架,为何依然能开发大型网站呢? 不过此图的原文却在推销使用Java,原文点此 ...

  9. [ZETCODE]wxWidgets教程二:辅助类

    本教程原文链接:http://zetcode.com/gui/wxwidgets/helperclasses/ 翻译:瓶哥 日期:2013年11月27日星期三 邮箱:414236069@qq.com ...

  10. [Ruby on Rails Issue] When Setting Sqlite version on the Gemfile, Show error "An error occurred while installing sqlite3 ",

    Issue: Gem files will remain installed in /tmp/bundler20140825-31835-p0c0p/sqlite3-1.3.9/gems/sqlite ...