hdu5637 Transform (bfs+预处理)
来源:互联网 发布:知乎关注最多的问题 编辑:程序博客网 时间:2024/05/19 17:49
Problem Description
A list of n integers are given. For an integer x you can do the following operations:
+ let the binary representation ofx be b31b30...b0¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ , you can flip one of the bits.
+ lety 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 .
+ let the binary representation of
+ let
There are several integer pairs
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 integern and m (1≤n≤15,1≤m≤105) -- the number of integers given and the number of queries. The next line contains n integers a1,a2,...,an (1≤ai≤105) , separated by a space.
In the nextm lines, each contains two integers si and ti (1≤si,ti≤105) , denoting a query.
The first line contains two integer
In the next
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
13 31 2 33 41 23 9
Sample Output
10
题意:给你n个数,有m个询问,每一个询问有两个值a,b,每一次操作,你可以把a中的二进制的一位异或1,即那位从0变为1或者1变为0,或者你可以异或上n个数中的一个数,问最小变化的次数。一开始打算m个询问直接模拟,但是发现时间爆了,所以采用预处理的方案,然后每次询问花O(1)的时间。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<bitset>#include<algorithm>using namespace std;typedef long long ll;typedef long double ldb;#define inf 99999999#define pi acos(-1.0)#define maxn 1000050#define MOD 1000000007int vis[1<<(20)],a[20],dis[1<<(20)];int q[1111111][2];int n;int maxx;void bfs(){ int i,j; memset(vis,0,sizeof(vis)); int front,rear,x,y,xx,yy,state1; front=rear=1; q[rear][0]=0;q[rear][1]=0; vis[0]=1;dis[0]=0; while(front<=rear) { int state=q[front][0]; int num=q[front][1]; front++; for(i=0;i<18;i++){ state1=(state^(1<<i)); if(vis[state1 ]==0 ){ dis[state1 ]=num+1; vis[state1 ]=1; if(state1>200050)continue; rear++; q[rear][0]=state1; q[rear][1]=num+1; } } for(i=1;i<=n;i++){ state1=(state^a[i]); if(vis[state1 ]==0 ){ dis[state1 ]=num+1; vis[state1 ]=1; if(state1>200005)continue; rear++; q[rear][0]=state1; q[rear][1]=num+1; } } }}int main(){ int m,i,j,T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); int maxx=0; for(i=1;i<=n;i++){ scanf("%d",&a[i]); } bfs(); ll sum=0; int c,d; for(i=1;i<=m;i++){ scanf("%d%d",&c,&d); sum=(sum+(ll)i*(ll)dis[c^d] )%MOD; } printf("%lld\n",sum); } return 0;}
0 0
- hdu5637 Transform (bfs+预处理)
- Transform HDU5637
- 【HDU5637 BestCoder Round 74 (div1)B】【bfs预处理】Transform 15个值+17个2的幂数最小异或步数使得s变成t
- HDU5637 bestcoder#74 Transform 题解&代码
- BestCoder Round #74 (div.2 c) hdu5637 Transform 【spfa】
- hdoj 5637 Transform 【BFS】
- poj_3669_Meteor Shower(BFS+预处理)
- HDU 3567 BFS+预处理
- poj 3669 bfs+预处理
- BFS+预处理 POJ
- Travel (BFS + 预处理优化)
- HihoCoder 1251 BFS预处理
- HDU 5637 Transform(bfs)
- uva 11624 Fire!(bfs预处理)
- 【NOIP2013】华容道 SPFA+BFS预处理
- HDU 3533 Escape 预处理+bfs
- 计蒜客 逃跑 (bfs+预处理)
- scikit-learn数据预处理fit_transform()与transform()的区别
- 【NOIP2012提高组】开车旅行
- dynamic
- 新工具︱微软Microsoft Visual Studio的R语言模块下载试用Ing...(尝鲜)
- 决策树之CART算法
- Dual-Source Deep Neural Networks for Human Pose Estimation 论文总结
- hdu5637 Transform (bfs+预处理)
- cxf webService异步调用模式
- ML for hackers 第一章笔记
- 关于排序的几种练习.
- 图像增强算法实现--图像的椒盐噪声、随机噪声、高斯噪声、均值滤波、中值滤波以及高斯滤波
- Bootstrap的一个很漂亮的web万能模板
- AlphaGo 开源项目研究(2)
- Leetcode 147. Insertion Sort List(链表实现快排)
- android weight(权重)的具体分析