hdu5637 Transform (bfs+预处理)

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 xy, 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 (T20), indicating the number of test cases. For each test case:

The first line contains two integer n and m (1n15,1m105) -- the number of integers given and the number of queries. The next line contains n integers a1,a2,...,an (1ai105), separated by a space.

In the next m lines, each contains two integers si and ti (1si,ti105), denoting a query.
 

Output
For each test cases, output an integer S=(i=1mizi) 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

题意:给你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 1000000007
int 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;
}


posted @ 2016-03-16 19:45  Herumw  阅读(119)  评论(0编辑  收藏  举报