Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF623E Transforming Sequence

LuoGu: CF623E Transforming Sequence

CF: E. Transforming Sequence

$f _ {i, j}$ 表示前 $i$ 个数有 $j$ 个 $1$ 。那么有 $f _ {i, j} = \sum _ {k = 0} ^ {j - 1} \binom j k 2 ^ k f _ {i - 1, k}$ ,也有 $f _ {n + m, j} = \sum _ {k = 1} ^ j \binom j k (2 ^ k) ^ m f _ {n, k} f _ {m, j - k}$ 。于是可以倍增。

查看代码
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
typedef __int128 L;
typedef long long LL;
const int N = 5e5 + 10, p = 1e9 + 7, p1 = 998244353, p2 = 1004535809, p3 = 469762049;
const L inf = (L)N * p * p, mul = (L)p1 * p2 * p3;
int binpow (int b, int k = p - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % p)
if (k & 1) res = (LL)res * b % p;
return res;
}
int fact[N], ifact[N];
template <const int &mod>
struct NTT
{
int rev[N];
int binpow (int b, int k = mod - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
void ntt (int *x, int bit, int op)
{
int tot = 1 << bit;
for (int i = 0; i < tot; ++i)
if ((rev[i] = rev[i >> 1] >> 1 | ((i & 1) << bit - 1)) > i)
swap(x[rev[i]], x[i]);
for (int mid = 1; mid < tot; mid <<= 1)
{
int w1 = binpow(3, (mod - 1) / (mid << 1));
if (!~op) w1 = binpow(w1);
for (int i = 0; i < tot; i += mid << 1)
for (int j = 0, k = 1; j < mid; ++j, k = (LL)k * w1 % mod)
{
int p = x[i | j], q = (LL)k * x[i | j | mid] % mod;
x[i | j] = (p + q) % mod, x[i | j | mid] = (p - q) % mod;
}
}
if (~op) return;
int itot = binpow(tot);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * itot % mod;
}
void PolyMul (int n, int *f, int m, int *g, int nm, int *w)
{
static int A[N], B[N];
for (int i = 0; i < n; ++i) A[i] = f[i] % mod;
for (int i = 0; i < m; ++i) B[i] = g[i] % mod;
int bit = 1;
while (n + m - 1 > 1 << bit) ++bit;
int tot = 1 << bit;
for (int i = n; i < tot; ++i) A[i] = 0;
for (int i = m; i < tot; ++i) B[i] = 0;
ntt(A, bit, 1), ntt(B, bit, 1);
for (int i = 0; i < tot; ++i)
w[i] = (LL)A[i] * B[i] % mod;
ntt(w, bit, -1);
for (int i = 0; i < nm; ++i) w[i] = (w[i] + mod) % mod;
for (int i = nm; i < tot; ++i) w[i] = 0;
}
};
NTT <p1> q1; NTT <p2> q2; NTT <p3> q3;
const L k1 = (L)p2 * p3 * q1.binpow((LL)p2 * p3 % p1), k2 = (L)p1 * p3 * q2.binpow((LL)p1 * p3 % p2), k3 = (L)p1 * p2 * q3.binpow((LL)p1 * p2 % p3);
int CRT (int x1, int x2, int x3)
{
L res = (x1 * k1 + x2 * k2 + x3 * k3) % mul;
if (res >= inf) res -= mul;
if (res <= -inf) res += mul;
return res % p;
}
void PolyMul (int n, int *f, int m, int *g, int nm, int *w)
{
static int A[N], B[N], C[N];
q1.PolyMul(n, f, m, g, nm, A);
q2.PolyMul(n, f, m, g, nm, B);
q3.PolyMul(n, f, m, g, nm, C);
for (int i = 0; i < nm; ++i)
w[i] = CRT(A[i], B[i], C[i]);
}
void PolyCalc (int n, LL m, int *g)
{
if (m == 1)
{
for (int i = 1; i < n; ++i) g[i] = 1;
return;
}
PolyCalc(n, m >> 1, g);
static int A[N], B[N];
A[0] = B[0] = 0;
for (int i = 1, s = binpow(2, (m >> 1) % (p - 1)), t = s; i < n; ++i, t = (LL)t * s % p)
{
A[i] = (LL)g[i] * t % p * ifact[i] % p;
B[i] = (LL)g[i] * ifact[i] % p;
}
PolyMul(n, A, n, B, n, A);
for (int i = 1; i < n; ++i)
g[i] = (LL)A[i] * fact[i] % p;
if (!(m & 1)) return;
A[0] = B[0] = 0;
for (int i = 1; i < n; ++i)
{
A[i] = (LL)g[i] * binpow(2, i) % p * ifact[i] % p;
B[i] = ifact[i];
}
PolyMul(n, A, n, B, n, A);
for (int i = 1; i < n; ++i)
g[i] = (LL)A[i] * fact[i] % p;
}
void init ()
{
fact[0] = 1;
for (int i = 1; i < N; ++i)
fact[i] = (LL)fact[i - 1] * i % p;
ifact[N - 1] = binpow(fact[N - 1]);
for (int i = N - 1; i; --i)
ifact[i - 1] = (LL)ifact[i] * i % p;
}
int main ()
{
init();
LL n; int m;
static int A[N];
read(n, m);
PolyCalc(m + 1, n, A);
int res = 0;
for (int i = 1; i <= m; ++i)
res = (res + (LL)A[i] * ifact[i] % p * ifact[m - i]) % p;
write(((LL)res * fact[m] % p + p) % p);
return 0;
}