【51nod】1407 与与与与
【51nod】1407 与与与与
设\(f(x)\) 为\(A_{i} \& x == x\)的\(A_{i}\)的个数
设\(g(x)\)为\(x\)里1的个数
\(\sum_{i = 0}^{2^{20}} (-1)^{g(x)}2^{f(x)}\)
\(f(x)\)就是按位取反之后的一个FMT卷积,把判断条件改成这一位不存在即可
也可以用FWT的与卷积直接卷起来
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N;
int a[(1 << 20) + 5],cnt[(1 << 20) + 5],pw[1000005];
int lowbit(int x) {
return x & (-x);
}
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
void Solve() {
memset(a,0,sizeof(a));
int d;
pw[0] = 1;
for(int i = 1 ; i <= N ; ++i) {
read(d);a[d]++;
pw[i] = mul(pw[i - 1],2);
}
for(int j = 0 ; j < 20 ; ++j) {
for(int S = (1 << 20) - 1 ; S >= 0 ; --S) {
if(!((S >> j) & 1)) {
a[S] += a[S ^ (1 << j)];
}
}
}
int ans = 0;
for(int S = 0 ; S < (1 << 20) ; ++S) {
if(S) cnt[S] = cnt[S - lowbit(S)] + 1;
int t;
if(cnt[S] & 1) t = MOD - 1;
else t = 1;
update(ans,mul(t,pw[a[S]]));
}
out(ans);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
while(scanf("%d",&N) != EOF) Solve();
}