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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,l,r,a[100],num[300010],flag[300010];
const int mod=1e9+7;
queue<int>qu;
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<n;i++)
       {
           if(!flag[a[i]^top])
           {
               qu.push(a[i]^top);
               num[a[i]^top]=num[top]+1;
               flag[a[i]^top]=1;
           }
       }
   }
}
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&m);
       for(int i=0;i<n;i++)
       {
           scanf("%d",&a[i]);
       }
       bfs();
       ll ans=0;
       for(int i=1;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 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值