用于解决集合逻辑运算的算法。。
就是能够造出类似多项式卷积的形式
⊕可以是 &^|。。。
证明解释什么的。。找了大半天了,也没找到个自己能看懂的,,好吧,,背模板
题面:
给出n个小于2^20的数,指定&^|其中之一为操作opt,求给定的n个数中任选两个,经过opt操作后能得到的最大值是多少???
solution:
记fi:数字i出现的次数+1
令g = f*f,就是上头那张图的形式,对于gi > 1,那么i就是能构造出的数
FWT就理解成和FFT差不多吧,,,,先变换成点值表达式,然后O(n)乘法,然后变回来(此处为口胡)
如何变换???背模板吧,,,
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int T = 20;
const int maxn = 1<<T;
int n,t,typ,c[maxn];
void fwt_xor(int *f)
{
for (int i = 0; i < T; i++)
for (int j = 0; j < maxn; j++) {
if (j & (1<<i)) continue;
int u = f[j],v = f[j | (1<<i)];
f[j] = u + v; f[j | (1<<i)] = u - v;
}
}
void ifwt_xor(int *f)
{
for (int i = 0; i < T; i++)
for (int j = 0; j < maxn; j++) {
if (j & (1<<i)) continue;
int u = f[j],v = f[j | (1<<i)];
f[j] = (u + v) / 2; f[j | (1<<i)] = (u - v) / 2;
}
}
void fwt_and(int *f)
{
for (int i = 0; i < T; i++)
for (int j = 0; j < maxn; j++) {
if (j & (1<<i)) continue;
int u = f[j],v = f[j | (1<<i)];
f[j] = u + v; f[j | (1<<i)] = v;
}
}
void ifwt_and(int *f)
{
for (int i = 0; i < T; i++)
for (int j = 0; j < maxn; j++) {
if (j & (1<<i)) continue;
int u = f[j],v = f[j | (1<<i)];
f[j] = u - v; f[j | (1<<i)] = v;
}
}
void fwt_or(int *f)
{
for (int i = 0; i < T; i++)
for (int j = 0; j < maxn; j++) {
if (j & (1<<i)) continue;
int u = f[j],v = f[j | (1<<i)];
f[j] = u; f[j | (1<<i)] = v + u;
}
}
void ifwt_or(int *f)
{
for (int i = 0; i < T; i++)
for (int j = 0; j < maxn; j++) {
if (j & (1<<i)) continue;
int u = f[j],v = f[j | (1<<i)];
f[j] = u; f[j | (1<<i)] = v - u;
}
}
int getint()
{
char ch = getchar();
int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("maximum.in","r",stdin);
freopen("maximum.out","w",stdout);
#endif
t = getint();
while (t--) {
n = getint();
typ = getint();
memset(c,0,sizeof(c));
for (int i = 0; i < n; i++) {
int x = getint();
c[x] = min(2,c[x] + 1);
}
if (typ == 1) fwt_and(c);
else if (typ == 2) fwt_xor(c);
else fwt_or(c);
for (int i = 0; i < maxn; i++) c[i] = c[i]*c[i];
if (typ == 1) ifwt_and(c);
else if (typ == 2) ifwt_xor(c);
else ifwt_or(c);
int ans = 0;
for (int i = 0; i < maxn; i++)
if (c[i] > 1) ans = i;
cout << ans << endl;
}
return 0;
}