跳到正文

学习·文化课

关于烷烃的同构异形体的计数

DanseSolo约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年3月18日
收录
2026年9月4日
更新
2026年11月19日
篇幅
约 13 分钟

版本

  1. v1当前

P6598 烷烃计数

其实就是上课讲了同构异形体然后就想怎么做然后没想出来然后就来锣鼓上搜然后发现是一道黑然后放弃了…

C++
#include<bits/stdc++.h>
using namespace std;

// 常量定义
const int MAX_BIT = 18;
const int MAX_BIT_ = 17;
const int MOD = 998244353;

// 类型重定义
typedef unsigned char u8;
typedef unsigned int u32;
typedef long long ll;
typedef unsigned long long ull;

// 快速读入结构体
struct FastRead {
	char ch;
	inline FastRead() { ch = getchar(); }
	
	template<typename T>
	inline FastRead& operator>>(T& num) {
		while (ch < '0' || ch > '9') ch = getchar();
		for (num = 0; ch >= '0' && ch <= '9'; ch = getchar())
			num = num * 10 + (ch - '0');
		return *this;
	}
} fast_cin;

// 全局数组
u32 root[1 << MAX_BIT];
u32 inv_root[1 << MAX_BIT];
u32 bit_rev[1 << (MAX_BIT + 1)];
u32 f[1 << MAX_BIT];
u32 a[1 << MAX_BIT];
u32 b[1 << MAX_BIT];
u32 c[1 << MAX_BIT];
u32 d[1 << MAX_BIT];
u32 e[1 << MAX_BIT];

// 快速幂(移除constexpr,修复C++14语法限制)
inline ull qpow(ull a, u32 b) {
	ull res = 1;
	for (; b; b >>= 1) {
		if (b & 1) res = res * a % MOD;
		a = a * a % MOD;
	}
	return res;
}

// 预计算逆元
const ull inv2 = qpow(2, MOD - 2);
const ull inv3 = qpow(3, MOD - 2);

// 初始化NTT数组(修复宏格式)
#define init_ntt() {                              \
	const ull g = qpow(3, (MOD - 1) >> MAX_BIT);     \
	const ull ig = qpow(g, MOD - 2);                 \
	u32 lev, i;                                      \
	root[1 << MAX_BIT_] = inv_root[1 << MAX_BIT_] = 1; \
	bit_rev[1] = 0;                                  \
	for (lev = 1, i = 2; lev <= MAX_BIT; ++lev)      \
		for (; i != (1u << (lev + 1)); ++i)          \
			bit_rev[i] = (i & 1) << (lev - 1) | bit_rev[i >> 1]; \
	for (i = (1 << MAX_BIT_) + 1; i < (1 << MAX_BIT); ++i) { \
		root[i] = (ull)g * root[i - 1] % MOD;             \
		inv_root[i] = (ull)ig * inv_root[i - 1] % MOD;    \
	}                                                \
	for (i = (1 << MAX_BIT_) - 1; i; --i) {         \
		root[i] = root[i << 1];                      \
		inv_root[i] = inv_root[i << 1];              \
	}                                                \
}

// 临时变量声明宏(包含所有需要的变量)
#define load()                    \
	ull v1, v2;                   \
	u32 st, pos, len, i, lev;

// NTT循环宏(修复变量作用域)
#define ntt_loop(size)                              \
	for (len = 1; len < (size); len <<= 1)          \
		for (st = 0; st < (size); st += len << 1)   \
			for (pos = 0; pos < len; ++pos)

// NTT核心操作宏
#define ntt_op(arr, w) {                         \
	v1 = arr[st | pos];                             \
	v2 = arr[st | pos | len];                       \
	v2 = (ull)v2 * w[pos | len] % MOD;              \
	arr[st | pos] = (v1 + v2) % MOD;                \
	arr[st | pos | len] = (v1 + MOD - v2) % MOD;    \
}

// 位逆序索引宏
#define rev_id(size, i) (bit_rev[(i) | (size)])

// 数组置零宏
#define clear_high(arr, size) memset(arr + (size), 0, (size) * sizeof(u32))
#define clear_arr(arr, size) memset(arr, 0, (size) * sizeof(u32))

// 循环宏
#define for_range(size) for (i = 0; i < (size); ++i)

// 修复核心:牛顿迭代宏改为纯语句,避免语法冲突
#define newton_iter_start(size) \
	ull inv_s = inv2;           \
	u32 cur_s = 2;
#define newton_iter_cond(size) ((cur_s >> 2) < (size))
#define newton_iter_step()      \
	cur_s <<= 1;                \
	inv_s = (ull)inv_s * inv2 % MOD;

// 多项式求逆(彻底修复语法错误)
inline void poly_inv(u32 size) {
	b[0] = qpow(a[0], MOD - 2);
	load()
	
	// 替换newton_iter宏为显式循环,避免语法错误
	newton_iter_start(size);
	while (newton_iter_cond(size)) {
		newton_iter_step();
		
		for_range(cur_s >> 1) {
			st = rev_id(cur_s, i);
			c[st] = a[i] ? MOD - a[i] : 0;
			d[st] = b[i];
			st = rev_id(cur_s, i | (cur_s >> 1));
			c[st] = d[st] = 0;
		}
		
		ntt_loop(cur_s) ntt_op(c, root);
		ntt_loop(cur_s) ntt_op(d, root);
		for_range(cur_s) b[rev_id(cur_s, i)] = (ull)c[i] * d[i] % MOD;
		ntt_loop(cur_s) ntt_op(b, inv_root);
		clear_high(b, cur_s >> 1);
		
		for_range(cur_s) c[rev_id(cur_s, i)] = (ull)inv_s * b[i] % MOD;
		c[0] = (c[0] + 2) % MOD;
		
		ntt_loop(cur_s) ntt_op(c, root);
		for_range(cur_s) b[rev_id(cur_s, i)] = (ull)c[i] * d[i] % MOD;
		ntt_loop(cur_s) ntt_op(b, inv_root);
		clear_high(b, cur_s >> 1);
		
		for_range(cur_s >> 1) b[i] = (ull)inv_s * b[i] % MOD;
	}
}

// 牛顿迭代求烷基计数(彻底修复语法错误)
inline void calc_alkyl(u32 size) {
	f[0] = 1;
	load()
	
	// 替换newton_iter宏为显式循环
	newton_iter_start(size);
	while (newton_iter_cond(size)) {
		newton_iter_step();
		
		for_range(cur_s >> 1) {
			c[rev_id(cur_s, i)] = f[i];
			c[rev_id(cur_s, i | (cur_s >> 1))] = 0;
		}
		
		ntt_loop(cur_s) ntt_op(c, root);
		for_range(cur_s) e[rev_id(cur_s, i)] = (ull)c[i] * c[i] % MOD;
		ntt_loop(cur_s) ntt_op(e, inv_root);
		
		for_range(cur_s >> 1) {
			d[rev_id(cur_s, i)] = e[i] = (ull)inv_s * e[i] % MOD;
			a[i + 1] = (ull)(MOD - inv2) * (e[i] + (i & 1 ? 0 : f[i >> 1])) % MOD;
			d[rev_id(cur_s, i | (cur_s >> 1))] = 0;
		}
		
		ntt_loop(cur_s) ntt_op(d, root);
		for_range(cur_s) e[rev_id(cur_s, i)] = (ull)c[i] * d[i] % MOD;
		ntt_loop(cur_s) ntt_op(e, inv_root);
		
		a[0] = 1;
		poly_inv(cur_s >> 1);
		
		for_range(cur_s >> 1) {
			c[rev_id(cur_s, i)] = b[i];
			d[rev_id(cur_s, i + 1)] = (ull)inv3 * ((i % 3 ? 0 : f[i / 3]) + MOD - (ull)inv_s * e[i] % MOD) % MOD;
			st = rev_id(cur_s, i | (cur_s >> 1));
			c[st] = d[st] = 0;
		}
		
		d[rev_id(cur_s, cur_s >> 1)] = 0;
		d[0] = 1;
		
		ntt_loop(cur_s) ntt_op(c, root);
		ntt_loop(cur_s) ntt_op(d, root);
		for_range(cur_s) f[rev_id(cur_s, i)] = (ull)c[i] * d[i] % MOD;
		ntt_loop(cur_s) ntt_op(f, inv_root);
		clear_high(f, cur_s >> 1);
		
		for_range(cur_s >> 1) f[i] = (ull)inv_s * f[i] % MOD;
	}
}

int main() {
	init_ntt();
	
	u32 n, size = 1;
	fast_cin >> n;
	while (size <= n) size <<= 1;
	
	calc_alkyl(size >> 1);
	for (u32 i = (n >> 1) + 1; i < size; ++i) f[i] = 0;
	
	load()
	size <<= 1;
	
	// 构造f(x), f(x^2), f(x^3)
	for_range(size) {
		st = rev_id(size, i);
		c[st] = f[i];
		d[st] = (i & 1 ? 0 : f[i >> 1]);
		e[st] = (i % 3 ? 0 : f[i / 3]);
	}
	
	// NTT变换
	ntt_loop(size) ntt_op(c, root);
	ntt_loop(size) ntt_op(d, root);
	ntt_loop(size) ntt_op(e, root);
	
	// 计算最终多项式
	for_range(size) {
		v1 = (ull)c[i] * c[i] % MOD;
		a[rev_id(size, i)] = (
			(6ull * d[i] + v1) % MOD * v1 % MOD
			+ 3ull * d[i] * d[i] % MOD
			+ 8ull * e[i] * c[i] % MOD
		) % MOD;
	}
	
	// 逆NTT变换
	ntt_loop(size) ntt_op(a, inv_root);
	
	// 计算逆元并调整系数
	const ull inv_size = qpow(size, MOD - 2);
	const ull inv24 = qpow(24, MOD - 2);
	for_range(size) {
		a[i] = (ull)inv24 * ((ull)inv_size * a[i] % MOD + 6ull * (i & 3 ? 0 : f[i >> 2]) % MOD) % MOD;
	}
	
	// 计算答案
	ull ans = a[n - 1];
	if (!(n & 1)) { // 偶数容斥
		v1 = (ull)f[n >> 1] * f[n >> 1] % MOD;
		v2 = ((v1 + MOD - f[n >> 1]) % MOD) * inv2 % MOD;
		ans = (ans + MOD - v2) % MOD;
	}
	
	cout << ans << endl;
	return 0;
}
原文
存档评论1 条抓取于