958F3 - Lightsabers (hard)

从$n$种有颜色的球里面取$k$,相同颜色算一种,一个颜色多球算一种。有$m$种颜色,$n,m\leq 2\times 10^5$

假设某种颜色有$y$个,它的生成函数$=1+x+x^2..x^y$

启发式合并$FFT$,即先将长度小的多项式项乘放到堆里面。

  • 所得出的多项式保持长度$\leq 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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 6e5 + 10;
const int mod = 1009;
const double pi = acos(-1.0);

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}

struct Complex
{
double x, y;
Complex(double _x = 0.0, double _y = 0.0)
{
x = _x;
y = _y;
}

Complex operator-(const Complex &b) const
{
return Complex(x - b.x, y - b.y);
}

Complex operator+(const Complex &b) const
{
return Complex(x + b.x, y + b.y);
}

Complex operator*(const Complex &b) const
{
return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
}
};
int rev[N];
void FFT(Complex *A, int n, int inv)
{
for (int i = 0; i < n; i++)
if (i < rev[i])
swap(A[i], A[rev[i]]);
for (int l = 1; l < n; l <<= 1)
{
Complex temp(cos(pi / l), inv * sin(pi / l));
for (int i = 0; i < n; i += (l << 1))
{
Complex omega(1, 0);
for (int j = 0; j < l; j++, omega = omega * temp)
{
Complex x = A[i + j], y = omega * A[i + j + l];
A[i + j] = x + y;
A[i + j + l] = x - y;
}
}
}

if (inv == -1)
for (int i = 0; i < n; i++)
A[i].x = ll(A[i].x / n + 0.5) % mod;
}
int Ployinit(int n, int m)
{
int ML = 1, bit = 0;
while (ML < n + m)
ML <<= 1, bit++;
return ML;
}
void FFTX(Complex *a, int n, Complex *b, int m)
{
int ML = 1, bit = 0;
while (ML < n + m)
ML <<= 1, bit++;
for (int i = 0; i < ML; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
FFT(a, ML, 1);
FFT(b, ML, 1);
for (int i = 0; i < ML; i++)
a[i] = a[i] * b[i];
FFT(a, ML, -1);
}
struct Ploy
{
int id;
int siz;
bool operator<(const Ploy &X) const
{
return siz > X.siz;
}
};
vector<int> h[N];
priority_queue<Ploy> q;
vector<int> p;
Complex a[N], b[N];
int main()
{
int n = read(), m = read(), k = read();
for (int i = 1; i <= m; i++)
h[i].push_back(1);
for (int i = 1; i <= n; i++)
{
int x = read();
h[x].push_back(1);
}
for (int i = 1; i <= m; i++)
{
while (h[i].size() > k + 1)
h[i].pop_back();
if (h[i].size() > 1)
q.push((Ploy){i, h[i].size()});
}
int cnt = m;
while (q.size() >= 2)
{
Ploy f = q.top();
q.pop();
Ploy g = q.top();
q.pop();
int len1 = f.siz;
int len2 = g.siz;
int ML = Ployinit(len1, len2);
for (int i = 0; i < ML; i++)
a[i].x = a[i].y = b[i].x = b[i].y = 0;
for (int i = 0; i < len1; i++)
a[i].x = h[f.id][i];
for (int i = 0; i < len2; i++)
b[i].x = h[g.id][i];
//cout << n << " " << m << endl;
FFTX(a, len1, b, len2);
++cnt;
for (int i = 0; i <= min(len1 + len2, k + 2); i++)
h[cnt].push_back(a[i].x);
q.push((Ploy){cnt, h[cnt].size()});
}
Ploy f = q.top();
q.pop();
printf("%d\n", h[f.id][k]);
}