hdu5657(bfs)异或性质应用。

A list of nn integers are given. For an integer xx you can do the following operations: 

+ let the binary representation of xx be b31b30...b0¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯b31b30...b0¯, you can flip one of the bits. 
+ let yy be an integer in the list, you can change xx to x⊕yx⊕y, where ⊕⊕ means bitwise exclusive or operation. 

There are several integer pairs (S,T)(S,T). For each pair, you need to answer the minimum operations needed to change SS to TT.

Input

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

The first line contains two integer nn and mm (1≤n≤15,1≤m≤105)(1≤n≤15,1≤m≤105) -- the number of integers given and the number of queries. The next line contains nn integers a1,a2,...,ana1,a2,...,an (1≤ai≤105)(1≤ai≤105), separated by a space. 

In the next mm lines, each contains two integers sisi and titi (1≤si,ti≤105)(1≤si,ti≤105), denoting a query.

Output

For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7)S=(∑i=1mi⋅zi) mod (109+7), where zizi is the answer for ii-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$

题意::

 

两种操作,一种是是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都找出来;

异或运算真神奇;不过我不会.....啊啊啊啊;

代码:
 

#include 
using namespace std;
typedef long long ll;
int n,m,l,r,a[100],num[300010],flag[300010];
const int mod=1e9+7;
queuequ;
int bfs()
{
    memset(flag,0,sizeof(flag));
    for(int i=1;i<=2e5;i*=2)
    {
        a[n++]=i;//打表
    }
    qu.push(0);
    num[0]=0;
    flag[0]=1;
   while(!qu.empty())
   {
       int top=qu.front();
       qu.pop();
       for(int i=0;i

 

你可能感兴趣的:(bfs广搜,位运算)