题目链接:

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. Android中的常见通信机制和Linux中的通信机制

    Handler Handler是Android系统中的一种消息传递机制,起作用是应对多线程场景.将A进程的消息传递给B线程,实现异步消息处理.很多情况是将工作线程中需要更新UI的操作消息传递给UI主线 ...

  2. 几种session存储方式比较

    原文: http://blog.sina.com.cn/s/blog_495697e6010143tj.html 集群中session安全和同步是个最大的问题,下面是我收集到的几种session同步的 ...

  3. error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad Touch of exactly '120x120' pixels,in.pen format for ios versions >= 7.0

    error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad ...

  4. java jdbc连接数据库,Properties 属性设置参数方法

    今天在整合为数据库发现在配置中实现的赋值方式,可以用代码实现.特记录下共以后参考: 代码:        // 操作数据库        Connection conn; String strData ...

  5. [Catalan数]1086 栈、3112 二叉树计数、3134 Circle

    1086 栈 2003年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 栈是计算机中 ...

  6. Lifting the Stone(多边形重心)

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. debian切换sh shell到bash shell

    1 安装dpkg-reconfigure命令 切换到root账户即可. 2 dpkg-reconfigure dash 选择no

  8. [postfix]添加黑名单

    最近公司员工的邮箱总是收到twoomail.com的邀请邮件,邮箱服务器还没有添加过黑名单呢,就拿它开刀吧. 在主配置文件中添加如下配置 #vi /etc/postfix/main.cf #black ...

  9. 【python】-- web开发之CSS

    CSS CSS作用概述:(通俗的讲就是将HTML这个赤裸裸的“人”,穿上华丽的衣服) CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常 ...

  10. go语言之接口一

    在Go语言中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口 我们定义了一个File类,并实现有Read().Write().Seek().Close()等方法.设 想我们有如下接口 ...