UOJ # 577. Mutation

[Title Description:

21st century is the century of biology, genetic and biological evolution as the representative of the modern theory more and more into our field of vision.

As well known, the gene is a genetic factor, which records basic configuration and performance life. Therefore, biological evolution and genetic variation is closely related, gene mutation study approach to biological research has a vital role. Now, let's look at a model like this:

1, all the genes can be regarded as a binary or integer corresponding to the integer;

2, in a unit of time, may be reversed gene x in which one bit;

3, in one unit of time, may be subject to gene x gene library can infect any gene mutation influence of y and x XOR y.

Now given the gene pool can be infected, Q group asked each group is given an initial gene and gene termination, you were calculated for each variation of the minimum number of units of time to spend.

[Input Description:

Line 1 two integers N, Q;

2 N row separated by spaces each represent an integer of infection of the gene library;

The next two lines each integers Q S, T, respectively, with the initial gene terminator gene.

[Output] Description:

Q output line sequentially represents the time spent between the initial gene happened to terminate each gene.

[Sample input]:

3 3
1 2 3
3 4
1 2
3 9

[] Sample output:

2
1
2

[Time limit, and the range of data Description:

Time: 1s space: 256M

Data for 20%, N = 0;

40% more data, 1≤Q≤100, all expressed genes is not more than 10 ^ 4 of non-negative integer;

To 100% of the data, 0≤N≤20,1≤Q≤10 ^ 5, all expressed genes nonnegative integer not exceeding 10 ^ 6.

Thinking

Each number is used at most once up

Therefore set F i , j only make use before i th amount [ with body See side away ] to up to j of most small step number

So f 20 , S ^ T that is the answer

Code

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
 
int n,qq,s,t,a[75];
int dis[1<<20],k,x;

void bfs() {
	queue<int> q;
	memset(dis,-1,sizeof(dis));
	q.push(0);
	dis[0]=0;
	while(!q.empty()) {
		k=q.front();
		q.pop();
		for(int i=1; i<=n+20; i++) {
			x=k^a[i];
			if(dis[x]!=-1)
				continue;
			dis[x]=dis[k]+1;
			q.push(x);
		}
	}
}

int main () {
	scanf("%d%d",&n,&qq);
	for(int i=1; i<=n; i++)
		scanf("%d",&a[i]);
	a[n+1]=1;
	for(int i=n+2; i<=n+20; i++)
		a[i]=2*a[i-1];
	bfs();
	for(int i=1,s,t; i<=qq; i++) {
		scanf("%d%d",&s,&t);
		printf("%d\n",dis[s^t]);
	}
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11524963.html