[CodeForces 165E] Compatible Numbers(状压 DP) | 错题本

文章目录

  • 题目
  • 分析
  • 代码

题目

[CodeForces 165E] Compatible Numbers

分析

简单状压 DP,将 S − { i } S-\{i\} S{i} 的答案给 S S S 即可。

代码

#include 
#include 
#include 

const int MAXN = 1000000;
const int MAXA = 4000000;
const int LOG = 22;

int N;
int A[MAXN + 5];

int Num[(1 << (LOG + 1)) + 5];

int main() {
	scanf("%d", &N);
	memset(Num, -1, sizeof Num);
	for (int i = 1; i <= N; i++) {
		scanf("%d", &A[i]);
		Num[A[i]] = A[i];
	}
	int lim = (1 << (LOG + 1)) - 1;
	for (int i = 1; i <= lim; i++) {
		if (~Num[i]) continue;
		for (int j = 0; j <= LOG; j++)
			if (i & (1 << j))
				if (~Num[i ^ (1 << j)]) {
					Num[i] = Num[i ^ (1 << j)];
					break;
				}
	}
	for (int i = 1; i <= N; i++) {
		int s = 0;
		for (int j = 0; j <= LOG; j++)
			if (!(A[i] & (1 << j)))
				s |= (1 << j);
		printf("%d ", Num[s]);
	}
	return 0;
}

你可能感兴趣的:([,错题本,],#,状压,DP)