F1 - Korney Korneevich and XOR (easy version)(暴力+简单DP)

F1 - Korney Korneevich and XOR (easy version)(暴力+简单DP)

a[i]最大是500所以异或和会小于512,可以暴力搜索所有可以得到的异或结果查看是否存在,又子序列必须是递增的所以我们可以用得到对应异或和的序列的最后一位元素进行更新下一位可以得到的异或元素

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <stack>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <list>
#include <sstream>
#include <unordered_set>
const int inf = 0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
const long double PI = acos(-1);
using namespace std;
typedef pair<int, int>PII;

const int N = 512 + 10;
int x, f[N], n;
//f[i]代表异或和为i的时候存的序列的最后一位元素
//因为题目要求是递增序列求异或和所以要存当前位的元素用于比较
void solve() {
	memset(f, 0x3f, sizeof(f));//初始化为0x3f若可以由异或得到则更新
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x;
		f[x] = min(f[x], x);
		for (int j = 0; j <= 512; j++) {
			if (x >= f[j])f[j ^ x] = min(f[j ^ x], x);//越小越好因为越小可以异或的数多
		}
	}
	set<int>s;
	for (int i = 0; i <= 512; i++)if (f[i] != inf)s.insert(i);//寻找什么异或和在f中已经得到
	cout << s.size() << endl;
	for (auto it : s) {
		cout << it << ' ';
	}cout << endl;
}
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	solve();
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值