CSU 2055: Wells‘s Lottery

题目:

Description

As is known to all, Wells is impoverished. 
When God heard that, God decide to help the poor Wells from terrible condition.
One day Wells met God in dream, God gave Wells a secret number which will bought Wells a great fortune and gifted Wells an ablility of transforming two lottery tickets x, y into a lottery ticket z (meet the condition that z==x or y).

Wells realized that the number must be the result of lottery, which would be worth ¥5,000,000! Wells was very excited, and can't help sharing this thought. The secret number is X, and Wells has purchase N lottery tickets a1,a2,a3....aNa1,a2,a3....aN but has not received them yet. And if lucky enough, Wells could pick some numbers of them satisfy that b1orb2orb3....orbk=Xb1orb2orb3....orbk=X, k is the amount of picked numbers.

How ever the owner of the lottery shop called SDJ, who decided to modify the lottery tickets and made Wells lost his fortune. In order to save energy to modify the lottery tickets, SDJ want to know the minimum amount of modification of lottery tickets.

Input

The input only contains one line.
First line contains two positive integers N (N<=105)(N<=105),X (X<=109)(X<=109) ,N means as described above Second line contains N number a1,a2...aN(0<=ai<=109),aia1,a2...aN(0<=ai<=109),ai means the number on i-th lottery tickets.

Output

Output a line only contains minimum amount of modification of lottery tickets.

Sample Input

3 7 
4 2 1

Sample Output

1

题意:

有n个数,问需要删掉多少个数才能使得剩下的数无论怎么选,都不能使得或运算的总结果为x

思路:

对于(a|x) != x来说,a是肯定不会被选的,所以不用管,

剩下的数,每一个对x的某些位都有贡献,

计算x的不为0的每一位,看哪一位的数最少,这个数量就是答案了。

代码:

#include<iostream>
using namespace std;

int main()
{
	int n, x, a, num[32];
	cin >> n >> x;
	int ans = n;
	for (int i = 0; i < 32; i++)num[i] = 0;
	while (n--)
	{
		cin >> a;
		if ((a|x) != x)continue;
		for (int i = 0; i < 32; i++)num[i] += a % 2, a /= 2;
	}	
	for (int i = 0; i < 32; i++)
	{
		if (x % 2 && ans>num[i])ans = num[i];
		x /= 2;
	}
	cout << ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nameofcsdn/article/details/80494152