CF914G - Sum the Fibonacci

$(s_a|s_b)\&s_c\&(s_d\oplus s_e)=2^i$

$s_a \& s_b =0$

求$\sum Fib(s_a|s_b)\&Fib(s_c)\&Fib(s_d\oplus s_e)$

  • 第一个子集卷积,
  • 第三个$FWT$
  • 这三部分每部分再乘上$Fib[i]$
  • 整体卷两次积
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1 << 17;

const int mod = 1e9 + 7;
int read()
{
int x = 0, F = 1;
char d = getchar();
while (d < '0' || d > '9')
{
if (d == '-')
F = -1;
d = getchar();
}
while (d >= '0' && d <= '9')
x = (x << 1) + (x << 3) + d - '0', d = getchar();
return x * F;
}
int qpow(int a, int x, int mo)
{
int res = 1;
while (x)
{
if (x & 1)
res = 1ll * res * a % mo;
x >>= 1;
a = 1ll * a * a % mo;
}
return res;
}
int inc(int x, int y, int mo)
{
if (y < 0)
y += mo;
if (x + y >= mo)
x -= mo;
return x + y;
}
int Inv2;
void FWT(int *A, int n, int op, int t) //t=1 or t=2 and t=3 xor
{
for (int i = 2; i <= n; i <<= 1)
{
for (int j = 0, mid = i >> 1; j < n; j += i)
for (int k = 0; k < mid; k++)
{

if (t == 1)
A[j + mid + k] = inc(A[j + mid + k], A[j + k] * op, mod);
else if (t == 2)
A[j + k] = inc(A[j + k], A[j + mid + k] * op, mod);
else if (t == 3)
{
int x = A[j + k], y = A[j + mid + k];
if (op == 1)
A[j + k] = (x + y) % mod, A[j + mid + k] = (x - y + mod) % mod;
else
A[j + k] = 1ll * Inv2 * (x + y) % mod, A[j + mid + k] = 1ll * Inv2 * (x - y + mod) % mod;
}
}
}
}
void FWTX(int *A, int *B, int n, int t)
{
Inv2 = qpow(2, mod - 2, mod);
FWT(A, n, 1, t), FWT(B, n, 1, t);

for (int i = 0; i < n; ++i)
A[i] = 1ll * A[i] * B[i] % mod;
FWT(A, n, -1, t);
}
int F[21][N], G[21][N];
int res[21][N];
void FJU(int *a, int *b, int n, int *d)
{

int m = 1 << n;
for (int i = 0; i < m; i++)
F[__builtin_popcount(i)][i] = a[i];
for (int i = 0; i < m; i++)
G[__builtin_popcount(i)][i] = b[i];
for (int i = 0; i <= n; i++)
FWT(F[i], m, 1, 1), FWT(G[i], m, 1, 1);

for (int x = 0; x <= n; x++)
{
for (int i = 0; i <= x; i++)
for (int j = 0; j < m; j++)
res[x][j] = inc(res[x][j], 1ll * F[i][j] * G[x - i][j] % mod, mod);
FWT(res[x], m, -1, 1);
}
for (int i = 0; i < m; i++)
d[i] = res[__builtin_popcount(i)][i];
}

int a[N], b[N], d[N], c[N];
int Fib[N];
int main()
{
int n = read();
int m = 1 << 17;
Fib[0] = 0;
Fib[1] = 1;
for (int i = 2; i < m; i++)
Fib[i] = (Fib[i - 2] + Fib[i - 1]) % mod;
for (int i = 0; i < n; i++)
{
int x = read();
a[x]++;
b[x]++;
c[x]++;
}
FJU(a, b, 17, d);
FWTX(a, b, 1 << 17, 3);
for (int i = 0; i < m; i++)
d[i] = 1ll * d[i] * Fib[i] % mod;
for (int i = 0; i < m; i++)
a[i] = 1ll * a[i] * Fib[i] % mod;
for (int i = 0; i < m; i++)
c[i] = 1ll * c[i] * Fib[i] % mod;

FWTX(d, c, 1 << 17, 2);
FWTX(d, a, 1 << 17, 2);
int res = 0;
for (int i = 0; i < 17; i++)
res = inc(res, d[1 << i], mod);
printf("%d\n", res);
}