C

C - ORXOR

原题链接:传送门


人一我十, 人十我百,追逐青春的梦想,怀着自信的心,永不言弃!


暴力,位运算

分析

​ 由于数据范围比较小,N只有20 我们可以尝试枚举所有的方案,计将A 划分为一个或者多个区间段。我们可以尝试枚举N个数字中每两个数字之间是否需要进行分割。N个数字一共有(2^{n-1})种方案,因为每一个空档只有选/不选两种状态。

AC 代码

#include <bits/stdc++.h>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
#define FOR(i,a,b)  for(int i=(a); i< (b); ++i)
#define RFOR(i,b,a) for(int i=(b);i>=(a);i--)
#define REP(i,a,b)  for(int i=(a); i<=(b); ++i)
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
template<typename T>
void PrArr(const T a[] , int len){
	for(int i = 0;i <= len; i++)cout << a[i] << " ";
	cout << endl;
}
template<typename T>
void PrVec(const vector<T> a){
	for(auto it : a)cout << it << " ";
	cout << endl;
}
const int MAX = 0x7ffffff;
const int MIN = 0xcf;
int test;
void slove()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i = 0; i < n; i ++)
		cin >> a[i];

	int res = 2000000000; 
	for(int i = 0; i < 1 << (n - 1); i ++)
	{
		int xored = 0;
		int ored = 0;

		for(int j = 0; j <= n; j++)
		{
			if(j < n) ored |= a[j];
			if(j == n || (i >> j & 1))
				xored ^= ored, ored = 0;
		}
		res = min(res, xored);
	}
	cout << res << endl;
}
int main()
{
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed; // 在iomanip中
#endif
	SIS;slove();
#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms
";
#endif
}
原文地址:https://www.cnblogs.com/wlw-x/p/14602745.html