AtCoder Beginner Contest 197---C(二进制暴搜)

这篇博客探讨了一道计算机科学题目,涉及位操作和搜索算法。内容包括如何通过枚举分割序列的位置来找到使序列中各段位运算结果异或值最小的策略。博主分享了两种策略:深度优先搜索(DFS)和二进制枚举,并提供了代码实现。文章还提到了可能的边界条件错误和解决方案,如使用更大的常量代替可能导致溢出的INF。
摘要由CSDN通过智能技术生成

Problem Statement

Given is a number sequence AA of length NN.
Let us divide this sequence into one or more non-empty contiguous intervals.
Then, for each of these intervals, let us compute the bitwise OROR of the numbers in it.
Find the minimum possible value of the bitwise XORXOR of the values obtained in this way.

Constraints

  • 1≤N≤201≤N≤20
  • 0≤Ai<2300≤Ai<230
  • All values in input are integers.

Sample Input 1

3
1 5 7

Sample Output 1 

2

If we divide [1,5,7][1,5,7] into [1,5][1,5] and [7][7], their bitwise ORORs are 55 and 77, whose XORXOR is 22.
It is impossible to get a smaller result, so we print 22.

 

 

数据范围 20 

所以是个暴搜题,枚举n-1个位置的插条,插和不插

1 暴搜策略dfs

2 2进制枚举

   思路:

   枚举每一位tmp = tmp | a[i]  遇到1,即插了一个板子的时候 加入结果ans ^= tmp

   边界条件的考虑: 最后一位时要加入结果

   坑: INF 会被卡掉0x3f3f3f3f不够大导致的,把INF改成INF+INF就过了。

code:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 100003
#define mod 100000
#define pb push_back
#define x first
#define y second
#define ull unsigned long long
#define ll long long
using namespace std;
int a[N];
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
	int n;
	cin >> n;
	for(int i = 0;i < n;i++)
	{
		cin >> a[i];
	}
	
	int mn = INF + INF ;
	for(int i = 0;i < (1 << (n - 1));i++)
	{
		int tmp = 0;
		int res = 0;
		for(int j = 0;j <= n;j++)
		{
			if(j < n)
			tmp |=  a[j];
			if(j == n || ((i >> j) & 1))
			{
				res ^= tmp;
				tmp = 0;
			}
		}
		mn = min(res,mn);
	}
	cout << mn << endl;
    return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值