HDU - 5637

时间:2019-01-17
本文章向大家介绍HDU - 5637,主要包括HDU - 5637使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Transform

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 918    Accepted Submission(s): 354


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 n integers 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

Hint

$3 \to 4$ (2 operations): $3 \to 7 \to 4$

$1 \to 2$ (1 operation): $1 \oplus 3 = 2$

$3 \to 9$ (2 operations): $3 \to 1 \to 9$

题意:

给出n个数的序列a,对于一个整数x,有两种操作: 
1.改变x二进制中任一位 
2.将x变为x^a[i]
m次查询,每次查询输入两个整数x和y,问x最少经过多少次操作可以变成y 

思路:因为求s^(t[1]^t[2]^...t[j])=t。那么转化成(t[1]^t[2]^...t[j])=s^t,那么就直接求0到s^t最少步数。

#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <time.h>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define PI acos(-1)
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;
const long long mod=1e9+7;
struct node
{
    int x,step;
};
int dis[1000001];
int a[20];
void bfs(int n)
{
    memset(dis,-1,sizeof(dis));
    dis[0] = 0;
    queue<node>q;
    node cur,nex;
    cur.x=cur.step=0;
    q.push(cur);
    int x=(1<<17);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(int i=0;i<n;i++)
        {
            nex.x=cur.x^a[i];
            nex.step=cur.step+1;
            if(nex.x<x&& dis[nex.x]==-1)
            {
                dis[nex.x]=nex.step;
                q.push(nex);
            }
        }
        for(int j=0;j<17;j++)
        {
            nex.x=cur.x^(1<<j);
            nex.step = cur.step+1;
            if(nex.x<x&&dis[nex.x]==-1)
            {
                dis[nex.x]=nex.step;
                q.push(nex);
            }
        }
    }
}
int main()
{
    int T, n, m, s, t;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        bfs(n);
        ll ans=0;
        for(int i=1;i<= m;i++)
        {
            scanf("%d%d",&s,&t);
            ans=(ans+(long long)i*dis[s^t])% mod;
        }
        printf("%lld\n",ans);
    }

    return 0;
}