版本比较
dmy4560 字字句句
正文
增加 11 行删除 11 行
1233456730 行未改动3839404142424344454612 行未改动596061626363646566676869707172727374757677787980808182838485868788888990919213 行未改动1061071081091101101111121131143 行未改动11811912012112212212312412512512612712812991 行未改动2212222232242252252262272282299 行未改动23924024124224324324424524624790 行未改动[原题链接](https://newoj.daimayuan.top/p/4560?tid=695f515386f7c22ec2ad9b37)
前面 $60\texttt{pts}$ 是用来拼暴力的,与正解关系不大,比较有启发的是 sub6。 前面 $60\texttt{pts}$ 是用来拼暴力的,与正解关系不大,比较有启发的是 sub6。
## sub6将递推式写成 GF,为:$F(x)=x+x\cdot F(x)+F(x^2)+F(x^3)$,移项得到 $F(x)=\frac{x+F(x^2)+F(x^3)}{1-x}$,最后我们所求的是 $[x^n]$ 的值。先拆一下,变为 $[x^n]\frac x{1-x}+[x^n]\frac{F(x^2)}{1-x}+[x^n]\frac{F(x^3)}{1-x}$,其中第一项是容易的,难点在于后面两个。
30 行未改动
## 实现细节上面只能算理论分析,代码方面还有一些问题。
注意到 $1-x-x^2-x^3$ 是有理数范围内的既约多项式,即意味着其所有零点都在复数范围内,并且输入计算器会发现结果相当复杂,根本不可能在 $\mathbb F_p$ 求得。转换思路,该多项式的三个零点 $a,b,c$ 并不是必须的,只是说作为理论分析比较方便,我们按照递推式去计算 $B_{i,j}$ 即可。 注意到 $1-x-x^2-x^3$ 是有理数范围内的既约多项式,即意味着其所有零点都在复数范围内,并且输入计算器会发现结果相当复杂,根本不可能在 $\mathbb F_p$ 求得。转换思路,该多项式的三个零点 $a,b,c$ 并不是必须的,只是说作为理论分析比较方便,我们按照递推式去计算 $B_{i,j}$ 即可。其中平方操作是容易的,$B_{i+1,j}(x)=B_{i,j}(x)B_{i,j}(-x)$。难点在于立方操作,$B_{i,j+1}(x)=B_{i,j}(x)B_{i,j}(\omega x)B_{i,j}(\omega^2x)$,发现 $\mathbb F_p$ 也不存在 $\omega$,需要扩域到 $\mathbb F_p(\omega)$ 下运算。
常数方面,注意到大部分运算还是整数,只有极少部分运算是扩域后的。这是因为 $B(\omega x)B(\omega^2x)$ 乘积的系数都是整数。扩域等价于一个二元组 $(a,b)$ 表示 $a+b\omega$,而二元组的乘法有 $4$ 次乘法操作,常数巨大,可以先判断一下是不是整数。
12 行未改动 int k = 0; while(1 << k + 1 <= n) k++; return k; }
LL ksm(LL x, LL y) { LL res = 1; x %= mod; while(y) { if(y & 1) res = res * x % mod; y >>= 1; x = x * x % mod; x = x * x % mod; } return res; }
struct Complex { int x, y;
Complex() {x = y = 0;} Complex(int x1) {x = x1, y = 0;} Complex(int x1, int y1) {x = x1, y = y1;} Complex operator-() const { return Complex(x ? mod - x : 0, y ? mod - y : 0); }
Complex operator+(const Complex &t) const { int a = x + t.x, b = y + t.y; return Complex(a < mod ? a : a - mod, b < mod ? b : b - mod);13 行未改动 Complex& operator+=(const Complex &t) {return *this = *this + t;} Complex& operator-=(const Complex &t) {return *this = *this - t;} Complex& operator*=(const Complex &t) {return *this = *this * t;} }omega(0, 1), omega2(mod - 1, mod - 1);
int bit, tot; int w[N], inv_num[N]; struct Initial {3 行未改动 w[tot >> 1] = ksm(3, mod - 1 >> bit + 1), w[0] = 1; for(int i = bit - 1; i; i--) w[1 << i - 1] = 1ll * w[1 << i] * w[1 << i] % mod; for(int i = 1; i < tot; i++) w[i] = 1ll * w[i & -i] * w[i ^ (i & -i)] % mod; inv_num[1] = 1; for(int i = 2; i <= tot; i++) inv_num[i] = 1ll * (mod - mod / i) * inv_num[mod % i] % mod; for(int i = 2; i <= tot; i++) inv_num[i] = 1ll * (mod - mod / i) * inv_num[mod % i] % mod; } }initial;
void DFT(Complex a[]) { for(int len = tot >> 1; len; len >>= 1) for(int i = 0, l = 0; i < tot; i += len << 1, l++)91 行未改动 pw[i][0] = 0; break; } Poly g = f[i - 1][0]; for(int j = 1; j < g.size(); j += 2) for(int j = 1; j < g.size(); j += 2) g[j] = -g[j]; Poly t = g * f[i - 1][0]; f[i][0].resize(f[i - 1][0].size()); for(int j = 0; j < f[i][0].size(); j++)9 行未改动 break; } Poly g1 = f[i][j - 1], g2 = f[i][j - 1]; Complex pw = 1; for(int k = 0; k < g1.size(); k++, pw *= omega) for(int k = 0; k < g1.size(); k++, pw *= omega) g1[k] *= pw; pw = 1; for(int k = 0; k < g2.size(); k++, pw *= omega2) g2[k] *= pw;90 行未改动