Codeforces Round #750 (Div. 2) F1. Korney Korneevich and XOR (easy version)

题目链接:https://codeforces.com/contest/1582/problem/F1

真的是菜的可以,容易想到ai<=500就是有个对512的遍历操作,但是怎么实现却一筹莫展。就一句话,dp[j]表示异或和结果为j的得到该结构的所有递增序列中最小的末尾值。

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;

int n, a[N], dp[N], ans[N], tot;

int main(void) {
//	freopen("in.txt", "r", stdin);
	// 1 2 4 8 16 32 64 128 256 512
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	memset(dp, INF, sizeof(dp));
	dp[0] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < 512; j++)
			if (a[i] > dp[j]) dp[j ^ a[i]] = min(dp[j ^ a[i]], a[i]);
	}
	for (int i = 0; i < 512; i++) {
		if (dp[i] != INF) ans[++tot] = i;
	}
	printf("%d\n", tot);
	for (int i = 1; i <= tot; i++)
		printf("%d%c", ans[i], i == tot ? '\n' : ' ');
	return 0;
}
上一篇:蓝桥杯递归算法


下一篇:C++二级题库刷题11