CF165E Compatible Numbers

题面

solution

(a& b = 0),设 (c)(a) 中去掉几个 (1)

(c & b = 0)

直接 (O(n)) 递推就好了。

(f[i | (1 << j)] = f[i])

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int read() {
  int x = 0, f = 1; char c = getchar();
  while(c < '0' || c > '9') {if (c == '-') f = -1;c = getchar();}
  while(c <= '9' && c >= '0') {x = x * 10 + c - '0';c = getchar();}
  return x * f;
}
int f[1 << 23], n, a[MAXN];
int main() {
  n = read();
  memset(f, -1, sizeof f);
  for (int i = 1; i <= n; i++) {
  	 a[i] = read();
  	 f[a[i] ^ ((1 << 23) - 1)] = a[i];
  }	
  for (int i = (1 << 23) - 1; i; i--) 
  	 for (int j = 0; j < 23; j++) 
  	   if(f[i | (1 << j)] != -1) f[i] = f[i | (1 << j)];	 
  for (int i = 1; i <= n; i++) cout<<f[a[i]]<<" ";
  return 0;
}
原文地址:https://www.cnblogs.com/Arielzz/p/15488226.html