题目链接:

Transform

Time Limit: 4000/2000 MS (Java/Others)    

Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 685    Accepted Submission(s): 244

Problem Description
A list of n integers are given. For an integer x you can do the following operations:

+ 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.

 
Input
There are multiple test cases. The first line of input contains an integer T (T≤20), indicating the number of test cases. For each test case:

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.

 
Output
For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7), where zi is the answer for i-th query.
 
Sample Input
1
3 3
1 2 3
3 4
1 2
3 9
 
Sample Output
10
 
 
题意:
 
两种操作,一种是是x^y,y是ai,还有一种是改变x的二进制位中的一位,相当于异或一个2的j次方(j=0,1,2,3,4...);
问s到t最少需要多少次,ans=sigama(i*zi)mod(1e9+7);
 
 
思路:
 
啊啊啊啊啊,自己又是不知道该怎么做,最后看了给的题解说只跟s^t有关才反应过来;这跟异或运算的性质有关;
s^x^y^z^w^...^q=t;假设这是最少的流程,等价于0^x^y^z^w..^q=s^t;就是0到s^t的最少次操作;然后用bfs把所有<=1e5都找出来;
异或运算真神奇;不过我不会.....啊啊啊啊;
 
 
AC代码:
 
#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)的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. POj 1753--Flip Game(位运算+BFS)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30669   Accepted: 13345 Descr ...

  3. HDU 5637 Transform

    题意: 有两种变换: 1. 改变此数二进制的某一位(1变成0 或者 0变成1) 2. 让它与给出的n个数当中的任意一个做异或运算 给你两个数s, t,求从s到t最少要经过几步变换,一共m组查询思路: ...

  4. HDU 5637 Transform 单源最短路

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

  5. HDU 5637 Transform 搜索

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

  6. hdu 5014(贪+位运算)

    题意:给你n+1个数(0->n),让你为这n+1个数在0->n中分别找一个数与其异或,求最后的最大值 思路:假设一个数5 (二进制1 0 1),则找的另一个数在5的0位上最好是1 , 1位 ...

  7. hdu 5637 Transform 最短路

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

  8. P1457 城堡 The Castle 位运算+BFS+思维(难题,好题)

    题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张"幸运爱尔兰"(一种彩票).结果这张彩票让他获得了这次 ...

  9. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

随机推荐

  1. 【BZOJ4927】第一题 双指针+DP(容斥?)

    [BZOJ4927]第一题 Description 给定n根直的木棍,要从中选出6根木棍,满足:能用这6根木棍拼 出一个正方形.注意木棍不能弯折.问方案数. 正方形:四条边都相等.四个角都是直角的四边 ...

  2. [Matlab绘图][三维图形][三维曲线基本函数+三维曲面+其他三维图形]

    1.绘制三维图形的基本函数 最基本的三维绘图函数为plot3: plot3与plot用法十分相似,调用格式: plot(x1,y1,z1,选项1,x2,y2,z2,选项2,...,xn,yn,zn,选 ...

  3. Nginx结合GeoIP库

    1. 编译nginx时带上geoip模块 # wget http://nginx.org/download/nginx-x.x.x.tar.gz # tar zxvf nginx-x.x.x.tar. ...

  4. litou.org.cn: A Shunde Program Fan.

    终于有了自己的域名litou.org.cn.(虽然并不是什么难的事) 作为顺德的一名编程爱好者,编程就是全部,希望可以多分享一些设计和开发经验. 一份耕耘,一份收获.My Name is 泥头.

  5. 【学员管理系统】0x03 老师信息管理功能

    [学员管理系统]0x03 老师信息管理功能 老师信息管理相比于学生信息管理又多了一点,因为我们的数据结构中老师表和班级表是通过teacher2class表进行多对多关联的. 写在前面 项目详细需求参见 ...

  6. crontab定时任务(待补充)

    cron是一个ubuntu下的后台进程,用来定期的执行一些任务 想让cron执行你指定的任务,首先就要编辑crontab文件.crontab是一个文本文件,用来存放你要运行的命令 第一种 vim /e ...

  7. mail

    mail.php  <?php require_once('class.phpmailer.php'); $mail = new PHPMailer(); //实例化 $mail->IsS ...

  8. HDU - 1175 连连看 【DFS】【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1175 思路 这种题一想到就用搜索, 但是内存是32m 用 bfs 会不会MLE 没错 第一次 BFS的 ...

  9. 在数据库中使用数字ID作为主键的表生成主键方法

    在数据库开发中,很多时候建一个表的时候会使用一个数字类型来作为主键,使用自增长类型自然会更方便,只是本人从来不喜欢有内容不在自己掌控之中,况且自增长类型在进行数据库复制时会比较麻烦.所以本人一直使用自 ...

  10. js 实现滑块效果

    var dd = $(".drag_bott").removeAttr('id').last().attr('id','drag_bott'); var drag = docume ...