题目链接:
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 685 Accepted Submission(s): 244
#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; }
原文:http://www.cnblogs.com/zhangchengc919/p/5331117.html