CodeForces 1395C-Boboniu and Bit Operations(位运算-暴力)

题目链接:https://codeforces.com/problemset/problem/1395/C
博客园食用链接: https://www.cnblogs.com/lonely-wind-/p/13494811.html

Boboniu likes bit operations. He wants to play a game with you.

Boboniu gives you two sequences of non-negative integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an and b 1 , b 2 , … , b m b_1,b_2,…,b_m b1,b2,,bm.

For each i ( 1 ≤ i ≤ n ) i (1≤i≤n) i(1in), you’re asked to choose a j ( 1 ≤ j ≤ m ) a_j (1≤j≤m) aj(1jm) and let c i = a i & b j c_i=a_i\&b_j ci=ai&bj, where & denotes the bitwise AND operation. Note that you can pick the same j for different i’s.

Find the minimum possible c 1 ∣ c 2 ∣ … ∣ c n c_1|c_2|…|c_n c1c2cn, where | denotes the bitwise OR operation.

Input
The first line contains two integers n and m (1≤n,m≤200).

The next line contains n integers a 1 , a 2 , … , a n ( 0 ≤ a i < 29 ) a_1,a_2,…,a_n (0≤a_i<29) a1,a2,,an(0ai<29).

The next line contains m integers b 1 , b 2 , … , b m ( 0 ≤ b i < 29 ) b_1,b_2,…,b_m (0≤b_i<29) b1,b2,,bm(0bi<29).

Output
Print one integer: the minimum possible c 1 ∣ c 2 ∣ … ∣ c n c_1|c_2|…|c_n c1c2cn.

input
4 2
2 6 4 0
2 4
output
2

input
7 6
1 9 1 9 8 1 0
1 1 4 5 1 4
output
0

input
8 5
179 261 432 162 82 43 10 38
379 357 202 184 197
output
147

Note
For the first example, we have c 1 = a 1 & b 2 = 0 , c 2 = a 2 & b 1 = 2 , c 3 = a 3 & b 1 = 0 , c 4 = a 4 & b 1 = 0 c1=a1\&b2=0, c2=a2\&b1=2, c3=a3\&b1=0, c4=a4\&b1=0 c1=a1&b2=0,c2=a2&b1=2,c3=a3&b1=0,c4=a4&b1=0.Thus c 1 ∣ c 2 ∣ c 3 ∣ c 4 = 2 c1|c2|c3|c4=2 c1c2c3c4=2, and this is the minimal answer we can get.

题目大意:给你数列a,b,必须对每一个 a i a_i ai b j b_j bj中任选一个数得 c i = a i & b j c_i=a_i\&b_j ci=ai&bj,最后使得 c 1 ∣ c 2 ∣ . . . ∣ c n c_1|c_2|...|c_n c1c2...cn最小

emmmm,刚开始的思路就是暴力,也写对了,就是出了点小BUG,后面结果改成DP了。。。然后赛后就被fst了。。。写了个假DP。。。

它的数据范围很小,所以我们可以直接考虑枚举答案,不难看出答案的区间为 [ 0 , 1 < < 2 9 ) [0,1<<2^9) [0,1<<29),然后我们就来检查答案了,两层for过去,然后取一下位运算与得 x x x,接下来就是对二进制下的 x x x的每一位是否符合答案,如果 x x x的某一位存在1,而答案不存在,那么就说明了这个 a i & b j a_i\&b_j ai&bj是不合法的。那么只要有一个 a i & b j a_i\&b_j ai&bj是合法的,那么 c i c_i ci就是合法的。于是我们很容易写出代码了。

以下是AC代码:

#include 
using namespace std;

int a[300],b[300];

int main(int argc, char const *argv[])
{
	int n,m;
	scanf ("%d%d",&n,&m);
	for (int i=1; i<=n; i++)
		scanf ("%d",&a[i]);
	for (int i=1; i<=m; i++)
		scanf ("%d",&b[i]);
	int ans=0;
	for (int i=0; i<(1<<9); i++){
		int mk=0;
		for (int j=1; j<=n; j++){
			int flag=0;
			for (int k=1; k<=m; k++){
				int x=a[j]&b[k];
				for (int g=0; g<=9; g++){
					if ((x&(1<<g)) && !(i&(1<<g))) {flag++; break;}
				}
			}
			if (flag==m) {mk=1; break;}
		}
		if (!mk) {ans=i; break;}
	}
	printf("%d\n",ans);
	return 0;
}

你可能感兴趣的:(Codeforces,Codeforces)