Codeforces 959 F Mahmoud and Ehab and yet another xor task

Discription

Ehab has an array a of n integers. He likes the bitwise-xor operation and he likes to bother Mahmoud so he came up with a problem. He gave Mahmoud q queries. In each of them, he gave Mahmoud 2 integers l and x, and asked him to find the number of subsequences of the first l elements of the array such that their bitwise-xor sum isx. Can you help Mahmoud answer the queries?

A subsequence can contain elements that are not neighboring.

Input

The first line contains integers n and q (1 ≤ n, q ≤ 105), the number of elements in the array and the number of queries.

The next line contains n integers a1a2..., an (0 ≤ ai < 220), the elements of the array.

The next q lines, each contains integers l and x (1 ≤ l ≤ n0 ≤ x < 220), representing the queries.

Output

For each query, output its answer modulo 109 + 7 in a newline.

Examples

Input
5 5
0 1 2 3 4
4 3
2 0
3 7
5 7
5 8
Output
4
2
0
4
0
Input
3 2
1 1 1
3 1
2 0
Output
4
2

Note

The bitwise-xor sum of the empty set is 0 and the bitwise-xor sum of a set containing one element is that element itself.

 

 

    这不是一个前缀线性基SB题吗2333

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
const int maxn=100005;
int ci[maxn],n,m,a[maxn],R,N;
int B[maxn][22],lef[maxn],Q;
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void init(){ ci[0]=1; for(int i=1;i<=100000;i++) ci[i]=add(ci[i-1],ci[i-1]);}
inline void build(int x){
	memcpy(B[x],B[x-1],sizeof(B[x-1]));
	for(int i=20;i>=0;i--) if(a[x]&ci[i]){
		if(!B[x][i]){
			B[x][i]=a[x];
			break;
		}
		a[x]^=B[x][i];
	}
	lef[x]=lef[x-1]+(!a[x]);
}
inline int query(){
	for(int i=20;i>=0;i--) if(N&ci[i]) N^=B[R][i];
	return N?0:ci[lef[R]];
}
int main(){
	scanf("%d%d",&n,&Q),init();
	for(int i=1;i<=n;i++) scanf("%d",a+i),build(i);
	while(Q--){
		scanf("%d%d",&R,&N);
		printf("%d\n",query());
	}
	return 0;
}

  

posted @ 2018-04-14 20:39  蒟蒻JHY  阅读(426)  评论(0编辑  收藏  举报