题解:暴力子集卷积
I_Want_AK_IOI · · 题解
纯属搞笑的题解。
枚举子集的代码是这样的 j = (j - 1) & i。于是我们打出暴力代码发现只有 80pts。
于是我们开始卡常,手写快读快写,缓冲区一次读全即可。__int128 计算最后取模(非常重要)。使用 (--j) &= i 更快地枚举子集(似乎编译器优化后于原来的实现是等价的,但是用这个肯定没坏处)。两个数组使用 unsigned int 存,算乘法的时候转 unsigned long long(非常重要)。全程 unsigned,因为 signed 似乎有点慢(也许是评测波动)。
#include <stdio.h>
unsigned char buffer[12 << 21];
unsigned pos;
inline unsigned read() {
static unsigned char c;
static unsigned x;
x = 0;
while (((c = buffer[pos++]) ^ '0') > 9);
while (x = (x << 3) + (x << 1) + (c ^ '0'), ((c = buffer[pos++]) ^ '0') < 10);
return x;
}
inline void print(unsigned x) {
if (x > 9) print(x / 10);
buffer[pos++] = x % 10 ^ '0';
}
inline void write(unsigned x) {
if (x > 9) print(x / 10);
buffer[pos++] = x % 10 ^ '0';
buffer[pos++] = ' ';
}
signed main() {
fread(buffer, 1, 12 << 21, stdin);
static unsigned n, i = 0, j = 0;
static unsigned a[1 << 20], b[1 << 20];
static unsigned __int128 c;
n = read(), n = 1u << n;
while (a[i] = read(), ++i != n);
while (b[j] = read(), ++j != n);
write((unsigned long long)a[i = pos = 0] * b[0] % 1000000009);
while (c ^= c, (j = ++i) < n) {
while (c += (unsigned long long)a[j] * b[i ^ j], (--j) &= i);
write((c + (unsigned long long)a[0] * b[i]) % 1000000009);
}
__builtin_fwrite(buffer, 1, --pos, stdout);
return 0;
}
然后我们就可以通过了!5.00s 卡线。但是我看了 zzzYheng 的暴力过题代码后,大受启发。我们可以只枚举 i - topbit(i) 的子集,每层循环加俩值,后续似乎还可以进行更强劲的循环展开但是我懒得加了。加了这个优化后,编译器似乎会对 for 有比 while 更激进的优化,下面的代码可以跑到 4.18 秒。
#include <stdio.h>
unsigned char buffer[12 << 21];
unsigned pos;
inline unsigned read() {
static unsigned char c;
static unsigned x;
x = 0;
while (((c = buffer[pos++]) ^ '0') > 9);
while (x = (x << 3) + (x << 1) + (c ^ '0'), ((c = buffer[pos++]) ^ '0') < 10);
return x;
}
inline void print(unsigned x) {
if (x > 9) print(x / 10);
buffer[pos++] = x % 10 ^ '0';
}
inline void write(unsigned x) {
if (x > 9) print(x / 10);
buffer[pos++] = x % 10 ^ '0';
buffer[pos++] = ' ';
}
signed main() {
fread(buffer, 1, 12 << 21, stdin);
static unsigned n, i = 0, j = 0;
static unsigned a[1 << 20], b[1 << 20];
static unsigned __int128 c;
n = read(), n = 1u << n;
while (a[i] = read(), ++i != n);
while (b[j] = read(), ++j != n);
write((unsigned long long)a[i = pos = 0] * b[0] % 1000000009);
while (c ^= c, ++i < n) {
for (unsigned x = j = i - (1u << (31 - __builtin_clz(i))); j; (--j) &= x) {
c += (unsigned long long)a[j] * b[i ^ j] + (unsigned long long)a[i ^ j] * b[j];
}
write((c + (unsigned long long)a[0] * b[i] + (unsigned long long)a[i] * b[0]) % 1000000009);
}
__builtin_fwrite(buffer, 1, --pos, stdout);
return 0;
}
后续经过我也不知道是啥的玄学改动,LOJ 上跑下面的代码会飞快。https://loj.ac/s/2460328
// 子集卷积
#include <stdio.h>
unsigned char buffer[12 << 21];
unsigned pos;
unsigned read() {
static unsigned char c;
static unsigned x;
x = 0;
while (((c = buffer[pos++]) ^ '0') > 9);
while (x = (x << 3) + (x << 1) + (c ^ '0'), ((c = buffer[pos++]) ^ '0') < 10);
return x;
}
void write(unsigned x) {
if (x > 9) write(x / 10);
buffer[pos++] = x % 10 ^ '0';
}
signed main() {
fread(buffer, 1, 12 << 21, stdin);
static unsigned n, i = 0, j = 0;
static unsigned a[1 << 20], b[1 << 20];
static unsigned __int128 c;
n = 1u << read();
while (a[i] = read(), ++i != n);
while (b[j] = read(), ++j != n);
write((unsigned long long)a[i = pos = 0] * b[0] % 1000000009u);
while (c ^= c, ++i < n) {
for (unsigned x = j = i - (1u << (31 - __builtin_clz(i))); j; (--j) &= x) {
c += (unsigned long long)a[j] * b[i ^ j] + (unsigned long long)a[i ^ j] * b[j];
}
buffer[pos++] = ' ';
write((c + (unsigned long long)a[0] * b[i] + (unsigned long long)a[i] * b[0]) % 1000000009u);
}
fwrite(buffer, 1, pos, stdout);
return 0;
}