题目链接
异或的性质。 求s到t的最少步骤, 等价于求0到s^t的最少步骤。
通过最少的步骤达到s^t的状态, 等价于求0到s^t的最短路。 先将最短路求出来然后O(1)查询。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e6+5;
int vis[maxn], dis[maxn], a[16], head[maxn], num;
struct node
{
int to, nextt;
}e[maxn*10];
void init() {
mem1(head);
num = 0;
}
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void spfa() {
queue<pll> q;
q.push(mk(0, 0));
mem(vis);
mem2(dis);
dis[0] = 0;
vis[0] = 1;
while(!q.empty()) {
pll tmp = q.front(); q.pop();
for(int i = head[tmp.se]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(vis[v])
continue;
vis[v] = 1;
if(dis[v]>dis[tmp.se]+1) {
dis[v] = dis[tmp.se]+1;
q.push(mk(-dis[v], v));
}
}
}
}
void solve() {
int n, m;
cin>>n>>m;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i <= 1e5; i++) {
for(int j = 0; j < n; j++) {
add(i, a[j]^i);
}
for(int j = 0; j < 17; j++) {
add(i, i^(1<<j));
}
}
spfa();
ll ans = 0;
int s, t;
for(int i = 1; i <= m; i++) {
scanf("%d%d", &s, &t);
s ^= t;
ans += i*dis[s];
ans %= mod;
}
cout<<ans<<endl;
}
int main()
{
int t;
cin>>t;
while(t--) {
init();
solve();
}
return 0;
}