d-finite 与 ODE 自动机
机械化求解整式递推,又称 ODE 自动机
最后还是要自己写一个(
用别人的不放心!
你就放心吧,我会写使用说明的(
定义
对函数 \(y(x)\),方程
为其的一个 \(n\) 阶线性微分方程。若 \(C(x) = 0\),则其为一个齐次的线性微分方程,并称满足这一方程的函数是微分有限(d-finite)的。若 \(\forall i, \text{deg }a_i(x) = 0\),则其是常系数的,反之则是变系数的。若对应函数是单变量的,就称对应的线性微分方程为常微分方程(ordinary differential equation,ODE)。
对数列 \(\{a_n\}\),方程
为其的一个 \(d\) 阶整式递推,其中 \(\forall p_i(n)\) 为多项式,并称满足这一方程的函数是可多项式递推(P-recursive)的。
可以证明:
-
令数列 \(\{a_n\}\) 的生成函数为 \(A(x)\),那么 \(A(x)\) d-finite,当且仅当 \(\{a_n\}\) P-recursive。
-
若 \(f(x), g(x)\) d-finite,则对任意常数 \(c_1, c_2\),\(c_1 f(x) + c_2 g(x)\) d-finite。
-
若 \(f(x), g(x)\) d-finite,则 \(f(x)g(x)\) d-finite。
-
若 \(f(x)\) d-finite,\(g(x)\) 代数,则 \(f(g(x))\) d-finite。其中,\(g(x)\) 代数,当且仅当存在不可约方程(不可被分解为两个次数较低的多项式之乘积的多项式与 \(0\) 形成的等式)
\[\sum_{i = 0}^n a_i(x) g^i(x) = 0 \]恒成立。
基础函数的构造
-
\(f(x) = x^{\alpha}\)
考察 \(x f'(x) = x\times \alpha x^{\alpha - 1} = \alpha x^{\alpha} = \alpha f(x)\),因此有\[\alpha f - x f' = 0 \] -
\(f(x) = e^x\)
考察 \(f'(x) = e^x = f(x)\),因此有\[f - f' = 0 \] -
\(f(x) = {}_pF_q(a_1,\dots, a_p; b_1, \dots, b_q; x)\)
令 \(\vartheta = xD\),则有\[(\vartheta + a_1)\cdots (\vartheta + a_p) f = D (\vartheta + b_1 - 1)\cdots (\vartheta + b_q - 1) f \] -
\(f(x) = c\times g(x)\),其中 \(c\) 为与 \(x\) 无关的常数。则 \(f(x)\) 满足的微分方程和 \(g(x)\) 相同。
写这个的原因是老是忘记常数咋处理。记住:微分方程不管常数,常数无论如何都能消掉,递推时它们的信息由初值给出。
如何实现?
一般而言,我们想要求得 ODE 的函数都是由上方基础函数加减乘除复合得来,因此我们只需要让机器帮我们做两个 ODE 相加、相乘,ODE 和代数函数复合,就基本上可以解决我们面对的问题。
受到我没看过的论文证明的启发,我们应当维护对应 d-finite 函数 \(f\) 的各阶导数 \(f, f', f'', \dots\) 组成的线性空间。
下面令系数都在模 \(p\) 意义下进行,即属于域 \(F_p\)。令我们要处理的两个 d-finite 函数为 \(f(x), g(x)\),并令
其中 \(\forall p_i(x), q_i(x) \in F_p(x)\),即系数均属于 \(F_p\) 的有理分式集。此外,不妨令 \(n\le m\)。
\(f(x) + g(x)\)
考察 \((f + g)^{(k)}\)。我们需要的就是用 \(f, \dots, f^{(n-1)}, g, \dots, g^{(m-1)}\) 表出每个 \((f + g)^{(k)}\)。
当 \(k < n\) 的时候,知道 \((f + g)^{(k)} = f^{(k)} + g^{(k)}\)。
当 \(k = n\) 的时候,\((f + g)^{(n)} = f^{(n)} + g^{(n)}\),而 \(f^{(n)}\) 可使用微分方程降次。
从而当 \(k \ge n\) 时,若
则
随后只需将 \(f^{(n)}\) 用微分方程降次即可。这样由归纳法可以知道,我们总能将每个 \((f + g)^{(k)}\) 用 \(f, \dots, f^{(n-1)}, g, \dots, g^{(m-1)}\) 表出。由于向量 \([f, \dots, f^{(n-1)}, g, \dots, g^{(m-1)}] ^{\mathsf T}\) 只有 \(n + m\) 维,则 \((f + g)^{(n + m)}\) 必定能由 \(f + g, (f + g)^{(1)}, \dots, (f + g)^{(n + m - 1)}\) 表出。
因此我们可以用与维护线性基类似的思想,一个个插入 \((f + g)^{(k)}\),最后必定可以得到一个 \((f + g)^{(k_0)}\) 使得其可以被线性表出,且 \(k_0 \le n + m\)。
\(f(x)g(x)\)
考察 \((fg)^{(k)}\) 和 \((fg)^{(k + 1)}\)。若
而 \(\left(a_{i,j}(x) f^{(i)} g^{(j)}\right)' = a_{i,j}'(x) f^{(i)} g^{(j)} + a_{i,j}(x) f^{(i + 1)} g^{(j)} + a_{i,j}(x) f^{(i)} g^{(j + 1)}\),并使用微分方程降次可以知道
也符合 \((fg)^{(k)}\) 的形式。这样由归纳法可以知道,我们总能将每个 \((f g)^{(k)}\) 用 \(fg, f^{(1)}g, \dots, f^{(n-1)}g, \dots, f^{(n-2)}g^{(m-1)}, f^{(n-1)}g^{(m-1)}\) 表出。由于以上的分量只有 \(n m\) 维,则 \((f g)^{(n m)}\) 必定能由 \(f g, (f g)^{(1)}, \dots, (f g)^{(n m - 1)}\) 表出。
因此我们可以用与维护线性基类似的思想,一个个插入 \((fg)^{(k)}\),最后必定可以得到一个 \((fg)^{(k_0)}\) 使得其可以被线性表出,且 \(k_0 \le nm\)。
\(f \circ g\)
这里 \(g(x)\) 符合一个代数方程 \(P(g) = 0\)。其系数 \(\in F_p(x)\)。
若 \(\text{deg } P = 1\),则 \(g(x)\) 也 \(\in F_p(x)\)。这时令
考察 \(\left(a_{k,i}(x)\left(f^{(i)} \circ g\right)\right)' = a_{k,i}'(x) \left(f^{(i)} \circ g\right) + a_{k,i}(x) \left(f^{(i + 1)} \circ g\right) g'\),当 \(i + 1 = n\) 时可以用微分方程降次,即
若 \(\text{deg } P > 1\),\(g(x)\) 不再具有简单的有理分式形式。上面的过程中有两个点无法直接解决:求解 \(g'\) 和求解每个 \(p_i\circ g\)。
若我们能将 \(g'\) 表示成关于 \(g\) 的某个整式 \(h(g)\),我们就能直接将其并入前一部分的 \(\left(f^{(i + 1)} \circ g\right)\),因此下面就是要用 \(g\) 表出 \(g'\)。
由于 \(P(g) = 0\),其对 \(x\) 求导得到的应为零函数。考察某项 \(a_i(x) g^i(x)\),其导数为 \(\left(a_i(x) g^i(x)\right)' = a_i'(x) g^i(x) + a_i(x) i g^{i - 1}(x) g'(x)\),前半部分求和得到 \(\dfrac{\partial P(t)}{\partial x} \circ g\),后半部分求和得到 \(\left(\dfrac{\partial P(t)}{\partial t}\circ g\right) g'\),其中 \(t\) 为占位元,与 \(x\) 无关。化简得到
这里视 \(P(g, x)\) 为二元代数函数,\(g,x\) 彼此无关。
为降次,把 \(g'\) (对应的 \(h(g)\))对 \(P(g)\) 取模。要算的就是同余方程
的解 \(y\)。可以证明这解必定存在,使用多项式扩展欧几里得即可。
对 \(p_i\circ g\),也可以对 \(P(g)\) 取模来降次。\(p_i(x) \in F_p(x)\),因此 \(p_i\) 分母 \(\in F_p[x]\)。而由于 \(P(u)\) 代数,其在系数所属的域 \(F_p[x]\) 上无法分解,故 \(P(u)\) 的根都在 \(F_p(x) / F_p[x]\) 上,其和 \(p_i(x)\) 的分母不存在公因式,可以求逆。
实现
实现了一个,写了 25k,全爆炸了。
以 EI 板子为基础构造代码。
一些细节。
有时候实现线性递推并不需要多项式模数,但由于一道题一般只有一个模数,可以和多项式板子共用一个模数。
需要系数在 \(F_p\) 上的多项式 opoly 类维护多项式。
需要 \(F_p(x)\) 上的有理分式,定义 pfrac 类维护。
需要系数在 \(F_(x)\) 上的多项式类维护 ODE。令对应的函数 \(f(x)\) 满足
并用 vector<opoly> 维护多项式列 \(p_i(x)\)。这里如果使用 \(f^{(n)} = \dots\) 的表达形式,则需要维护有理分式列,相对降低直观性。
最后就是用 ODE 返回整式递推,以及用整式递推 \(O(n)\) 推导了。
为了不让过长的前导零降低效率,维护 shrink(T& x) 删除 \(x\) 的前导零。
为统一接口,可以封装一个 \(F_p\) 内元素的类 Z,并在底层采用取模优化。由于此处常数瓶颈并非 ntt 而是线性的取模,预计这样的优化会减小常数。
注意:使用单个常数初始化 opoly 会得到对应常数长度的零多项式,得到常单项式 \(c\) 需要使用语法 opoly(1, c) 或直接使用 {Z(c)}。在隐性类型转换时(如多项式除以 int 等处)尤其要小心。
这里也暴露了一个问题:多项式本体 poly 的系数是 u32 类型的,并非 opoly 的 Z 类型。因此,需要在推导的某个阶段逐渐将 opoly 逐步更换为 poly,便于整式递推的获得与结果的处理。这个阶段难以确定,因此不妨定义成员函数 trans_poly 返回 poly 盛放的整式递推。
目前认为,将 ODE 处理的全部内容(包括 \(+\times\circ\),得到整式递推,执行 \(O(n)\) 递推)均封装为 ODE 的成员函数会相对增加直观性。
使用方法
先把板子放在这里。需要使用多项式类 poly,但功能似乎不是很大。
code
struct FastMod {
int m; i64 b;
inline void init(int _m = 1) { m = _m; if (m == 0) m = 1; b = ((__int128_t)1 << 64) / m; }
FastMod(int _m = 1) { init(_m); }
inline int operator()(i64 a) { i64 q = ((__int128_t)a * b) >> 64; a -= q * m; if (a >= m) a -= m; return a; }
} Mod(mod);
struct Z {
u32 v;
Z(u32 v = 0) : v(v) {}
Z(int v) : v(Norm(Mod(v) + mod)) { }
Z(long long v) : v(Norm(Mod(v) + mod)) { }
inline friend Z operator+(const Z& lhs, const Z &rhs) { return Norm(lhs.v + rhs.v); }
inline friend Z operator+(const Z& lhs, const int &rhs) { return lhs + Z(rhs); }
inline friend Z operator+(const int& lhs, const Z& rhs) { return Z(lhs) + rhs; }
inline friend Z operator-(const Z& lhs, const Z &rhs) { return Norm(lhs.v + mod - rhs.v); }
inline friend Z operator-(const Z& lhs, const int &rhs) { return lhs - Z(rhs); }
inline friend Z operator-(const int& lhs, const Z& rhs) { return Z(lhs) - rhs; }
Z operator-() const { return Norm(mod - v); }
inline Z inv() const {
if (!v) throw domain_error("zero field divisor");
return v == 1 ? Z(1) : Z(qp(*this, mod - 2));
};
inline friend Z operator*(const Z& lhs, const Z& rhs) { return Mod(1ll * lhs.v * rhs.v); }
inline friend Z operator*(const Z& lhs, const int &rhs) { return lhs * Z(rhs); }
inline friend Z operator*(const int& lhs, const Z& rhs) { return Z(lhs) * rhs; }
inline friend Z operator/(const Z& lhs, const Z &rhs) { return Mod(1ll * lhs.v * rhs.inv().v); }
inline friend Z operator/(const Z& lhs, const int &rhs) { return lhs / Z(rhs); }
inline friend Z operator/(const int& lhs, const Z& rhs) { return Z(lhs) / rhs; }
operator u32() const { return v; }
inline friend ostream &operator<< (ostream &out, const Z &x) { return out << x.v; }
};
inline Z &operator+=(Z &lhs, const Z &rhs) { return lhs = lhs + rhs; }
inline Z &operator-=(Z &lhs, const Z &rhs) { return lhs = lhs - rhs; }
inline Z &operator*=(Z &lhs, const Z &rhs) { return lhs = lhs * rhs; }
inline Z &operator/=(Z &lhs, const Z &rhs) { return lhs = lhs / rhs; }
struct opoly : vector<Z> {
opoly(const int& n = 1, const Z& val = 0) : vector(n, val) {}
opoly(const initializer_list<value_type> &il) : vector(il) {}
opoly(const vector<Z> &il) : vector(il) {}
inline int degree() const { return (int) size() - 1; }
inline bool shrink() { int k = size(); while (k && !at(k - 1)) --k; resize(k); return k; }
inline void redegree(const size_t& n) { resize(n + 1); }
inline opoly operator-() const { opoly ret(size()); for (int i = 0; i < size(); ++i) ret[i] = -at(i); return ret; }
inline operator poly() const { poly ret(size()); for (int i = 0; i < (int)size(); ++ i) ret[i] = u32(at(i)); return ret; }
inline friend opoly operator*(const opoly &a, const opoly &b) {
if (a.empty() || b.empty()) return opoly(0);
opoly c(a.size() + b.size() - 1);
for (int i = 0; i < a.size(); ++i) if (a[i] != 0)
for (int j = 0; j < b.size(); ++j) c[i + j] += a[i] * b[j];
return c.shrink(), c;
}
inline friend opoly operator*(const opoly &a, const Z &z) {
opoly c(a);
for (Z &x : c) x *= z;
return c.shrink(), c;
}
inline friend opoly operator*(const Z &z, const opoly &a) { return a * z; }
inline friend opoly operator+(const opoly &a, const opoly &b) {
opoly c(max(a.size(), b.size()));
for (int i = 0; i < a.size(); ++i) c[i] += a[i];
for (int i = 0; i < b.size(); ++i) c[i] += b[i];
return c.shrink(), c;
}
inline friend opoly operator-(const opoly &a, const opoly &b) { return a + -b; }
inline friend opoly operator+(const opoly &a, const Z &z) {
opoly c(a);
if (c.empty()) c.resize(1);
c[0] += z;
return c.shrink(), c;
}
inline friend opoly operator+(const Z &z, const opoly &a) { return a + z; }
inline friend opoly operator-(const opoly &a, const Z &z) { return a + -z; }
inline friend opoly operator-(const Z &z, const opoly &a) { return -a + z; }
inline friend bool operator==(const opoly &a, const opoly &b) {
for (int i = 0; i < max(a.size(), b.size()); ++i)
if ((i < a.size() ? a[i] : Z()) != (i < b.size() ? b[i] : Z())) return false;
return true;
}
opoly deri() const {
if (empty()) return *this;
opoly a(*this);
for (int i = 1; i < a.size(); ++i) a[i - 1] = a[i] * i;
a.pop_back();
return a;
}
Z eval(const Z &z) const {
Z v = 0;
for (int i = degree(); i >= 0; --i) v = v * z + at(i);
return v;
}
};
template <typename _vi>
inline opoly topoly(const _vi& a) {
opoly ret(a.size());
for (int i = 0; i < a.size(); ++ i)
ret[i].v = a[i];
return ret;
}
inline opoly gcd(opoly a, opoly b) {
a.shrink(), b.shrink();
while (!b.empty()) {
if (b.size() == 1) return opoly{Z(1)};
Z in = b.back().inv();
int n = a.degree(), m = b.degree();
for (int i = n; i >= m; --i) {
Z z = a[i] * in;
for (int j = 1; j <= m; ++j) a[i - j] -= z * b[m - j];
a[i] = 0;
}
a.shrink(), swap(a, b);
}
if (!a.empty() && a.back() != 1) a = a * a.back().inv();
return a;
}
inline opoly div(const opoly& _a, const opoly& _b) {
opoly a(_a), b(_b);
a.shrink(), b.shrink();
if (b.empty()) throw domain_error("zero polynomial divisor");
int n = a.degree(), m = b.degree();
if (n < m) return opoly(0);
if (m == 0) return b[0] == 1 ? a : a * b[0].inv();
Z in = b.back().inv();
opoly ret(n - m + 1);
for (int i = n; i >= m; --i) {
Z z = a[i] * in;
ret[i - m] = z;
for (int j = 1; j <= m; ++j) a[i - j] -= z * b[m - j];
}
return ret.shrink(), ret;
}
inline opoly operator/ (const opoly& _a, const opoly& _b) { return div(_a, _b); }
inline opoly& operator/= (opoly& _a, const opoly& _b) { _a = div(_a, _b); return _a; }
struct pfrac {
opoly x, y;
pfrac(const opoly &x = opoly(), const opoly &y = {Z(1)}) : x(x), y(y) { shrink(); }
void shrink() {
y.shrink();
if (y.empty()) throw domain_error("zero rational denominator");
if (!x.shrink()) { y = opoly{Z(1)}; return; }
if (y.size() > 1) {
opoly g = gcd(x, y);
x /= g, y /= g;
}
if (y.back() != 1) {
Z in = y.back().inv();
x = x * in, y = y * in;
}
}
pfrac operator+(const pfrac &rhs) const {
if (x.empty()) return rhs;
if (rhs.x.empty()) return *this;
if (y == rhs.y) return pfrac(x + rhs.x, y);
opoly g = gcd(y, rhs.y), a = div(y, g), b = div(rhs.y, g);
opoly u = x * b + rhs.x * a;
if (u.empty()) return pfrac();
opoly h = gcd(u, g);
pfrac ret;
ret.x = div(u, h), ret.y = a * div(rhs.y, h);
return ret;
}
inline pfrac operator-() const {
pfrac ret(*this);
ret.x = -ret.x;
return ret;
}
inline pfrac operator-(const pfrac &rhs) const { return *this + -rhs; }
pfrac operator*(const pfrac &rhs) const {
if (x.empty() || rhs.x.empty()) return pfrac();
opoly a = gcd(x, rhs.y), b = gcd(y, rhs.x);
pfrac ret;
ret.x = div(x, a) * div(rhs.x, b), ret.y = div(y, b) * div(rhs.y, a);
return ret;
}
pfrac inv() const { return pfrac(y, x); }
pfrac operator/(const pfrac &rhs) const { return *this * rhs.inv(); }
bool operator==(const pfrac &rhs) const { return y == rhs.y ? x == rhs.x : x * rhs.y == y * rhs.x; }
bool operator!=(const pfrac &rhs) const { return !operator==(rhs); }
pfrac deri() const {
if (x.empty()) return pfrac();
opoly g = gcd(y, y.deri()), a = div(y, g), b = div(y.deri(), g);
return pfrac(x.deri() * a - x * b, y * a);
}
};
struct Q_Basis {
int dim, id;
vector<vector<pfrac> > basis, augment;
Q_Basis(int dim) : dim(dim), id(), basis(dim), augment(dim) {}
vector<pfrac> insert(vector<pfrac> vec) {
vector<pfrac> tmp(dim + 1);
tmp[id++] = pfrac({Z(1)});
for (int i = 0; i < dim; ++i) {
if (!vec[i].x.empty()) {
if (basis[i].empty()) {
pfrac in = vec[i].inv();
for (int j = i + 1; j < dim; ++j) {
vec[j] = vec[j] * in;
}
for (int j = 0; j < id; ++j) {
tmp[j] = tmp[j] * in;
}
vec[i] = pfrac({Z(1)});
basis[i] = vec;
augment[i] = tmp;
return {};
} else {
for (int j = i + 1; j < dim; ++j)
vec[j] = vec[j] - vec[i] * basis[i][j];
for (int j = 0; j < id; ++j)
tmp[j] = tmp[j] - vec[i] * augment[i][j];
vec[i] = pfrac();
}
}
} return tmp;
}
};
using PRec = vector<opoly>;
using ODE_base = vector<opoly>;
long double recur_cost(int m, int d) {
if (m == 0) return 0;
if (m < 0 || d < 0) return numeric_limits<long double>::infinity();
if (d == 0) return 30.L * m * m / 32768.L;
return 1.L * m * m * sqrtl(d) * (m + 15.L + .5L * log2l(d));
}
pii recur_shape(const PRec &rec) {
int d = 0;
for (auto &p : rec) d = max(d, p.degree());
return {(int)rec.size() - 1, d};
}
enum class PRecStatus { optimal, bounded, impossible, unresolved };
struct PRecBudget {
int order = 17, degree = 8, columns = 256, trials = 128;
size_t cells = 200000;
int milliseconds = 2000;
};
struct PRecResult {
PRec rec;
PRecStatus status = PRecStatus::unresolved;
bool found() const { return !rec.empty(); }
bool proved() const { return status == PRecStatus::optimal || status == PRecStatus::bounded || status == PRecStatus::impossible; }
u64 zero = numeric_limits<u64>::max(), obstruction = 0;
long double weight = numeric_limits<long double>::infinity();
int max_order = 0, max_degree = 0, tested = 0;
bool global = false;
};
struct PRecData {
vector<poly> first, second;
vector<vector<poly> > candidates;
};
struct ODE {
ODE_base ode;
ODE(const int& n = 1, const opoly& val = {0}) : ode(n, val) {}
ODE(const initializer_list<opoly> &il) : ode(il) { shrink(); }
inline int size() const { return (int)ode.size(); }
inline int degree() const { return size() - 1; }
inline opoly& operator[](int x) {
_rec.clear(), _safe.clear(), _frontier.clear(), coef.clear();
return ode[x];
}
inline const opoly& operator[](int x) const { return ode[x]; }
inline ODE& resize(int x) {
_rec.clear(), _safe.clear(), _frontier.clear(), coef.clear();
return ode.resize(x), *this;
}
inline ODE& redegree(int x) { return resize(x + 1); }
inline ODE_base::iterator begin() { return ode.begin(); }
inline ODE_base::iterator end() { return ode.end(); }
void shrink() {
_rec.clear(), _safe.clear(), _frontier.clear(), coef.clear();
for (opoly &x : ode) x.shrink();
while (ode.size() > 1 && ode.back().empty()) ode.pop_back();
if (ode.empty()) ode.resize(1);
opoly g;
for (opoly &x : ode) g = gcd(g, x);
if (g.empty()) return;
for (opoly &x : ode) if (!x.empty()) x = div(x, g);
}
static ODE from_frac(const vector<pfrac> &v) {
opoly den{Z(1)};
for (auto &a : v) den = den * div(a.y, gcd(den, a.y));
ODE ret(v.size());
for (int i = 0; i < v.size(); ++i) ret[i] = v[i].x * div(den, v[i].y);
return ret.shrink(), ret;
}
ODE deri() const {
ODE _ode(*this);
_ode.resize(_ode.size() + 1);
for (int i = (int) _ode.size() - 2; i >= 0; --i) {
_ode[i + 1] = _ode[i + 1] + _ode[i];
_ode[i] = _ode[i].deri();
} return _ode;
}
ODE theta() const {
ODE _ode(deri());
for (int i = 0; i < _ode.size(); ++i) _ode[i] = _ode[i] * opoly{Z(), Z(1)};
return _ode;
}
PRec _rec = PRec(), _safe = PRec();
ODE_base _source;
vector<PRec> _frontier;
vector<Z> coef;
pair<PRec, PRec> reduce(const PRec &rec, int d = -1, vector<PRec> *frontier = nullptr) const {
int n = degree(), m = (int)rec.size() - 2;
if (d == -1) d = 2 * n;
if (frontier) frontier->push_back(rec);
if (n <= 0 || d < n || m <= 0 || 1ll * (m + 1) * (d + 1) > 120) return {rec, rec};
int mx = 0;
for (auto &a : ode) mx = max(mx, a.degree());
if (1ll * n * (d * (mx + 1) + m + 1) * (m + 1) * (d + 1) > 200000) return {rec, rec};
opoly h = ode[n], hd = h.deri(), x{Z(), Z(1)};
vector<opoly> pw(d + 1, opoly{Z(1)});
for (int i = 1; i <= d; ++i) pw[i] = pw[i - 1] * h;
vector<vector<opoly> > p(d + 1, vector<opoly>(n));
p[0][0] = opoly{Z(1)};
for (int i = 0; i < d; ++i)
for (int j = 0; j < n; ++j) {
p[i + 1][j] = x * (h * p[i][j].deri() - hd * p[i][j] * Z(i) - ode[j] * p[i][n - 1]);
if (j) p[i + 1][j] = p[i + 1][j] + x * h * p[i][j - 1];
}
vector<int> pos(n + 1);
for (int j = 0; j < n; ++j) {
opoly g;
for (int i = 0; i <= d; ++i) {
p[i][j] = p[i][j] * pw[d - i];
p[i][j].shrink();
g = gcd(g, p[i][j]);
}
int len = 0;
for (int i = 0; i <= d; ++i) {
if (!p[i][j].empty()) p[i][j] = div(p[i][j], g);
len = max(len, (int)p[i][j].size());
}
pos[j + 1] = pos[j] + len + m;
}
auto solve = [&](int cap, int limit, bool keep) {
int rows = pos[n], cols = (limit + 1) * (cap + 1);
vector<vector<Z> > basis(rows), augment(rows), kernel;
PRec first;
auto valid = [&](const vector<Z> &a) {
for (int i = 0; i <= (keep ? 0 : cap); ++i) if (a[i] != 0) return true;
return false;
};
for (int j = 0; j <= limit; ++j) {
for (int i = 0; i <= cap; ++i) {
if (keep && j == 0 && i != 0) continue;
int id = j * (cap + 1) + i;
vector<Z> vec(rows), tmp(cols);
tmp[id] = 1;
for (int k = 0; k < n; ++k) {
opoly a = p[i][k];
if (keep && j == 0) {
a = opoly();
for (int t = 0; t < rec[0].size(); ++t) a = a + p[t][k] * rec[0][t];
}
for (int t = 0; t < a.size(); ++t) vec[pos[k] + j + t] = a[t];
}
bool independent = false;
for (int k = 0; k < rows; ++k) if (vec[k] != 0) {
if (basis[k].empty()) {
Z in = vec[k].inv();
for (int t = k; t < rows; ++t) vec[t] *= in;
for (int t = 0; t <= id; ++t) tmp[t] *= in;
basis[k] = vec, augment[k] = tmp;
independent = true;
break;
}
Z z = vec[k];
for (int t = k; t < rows; ++t) vec[t] -= z * basis[k][t];
for (int t = 0; t <= id; ++t) tmp[t] -= z * augment[k][t];
}
if (!independent) kernel.push_back(tmp);
}
vector<vector<Z> > reduced = kernel;
vector<Z> ans;
for (auto &a : reduced) if (valid(a)) {
ans = a;
break;
}
if (ans.empty()) continue;
for (int t = cap; t > n; --t) {
for (int k = keep ? 1 : 0; k <= j; ++k) {
int id = k * (cap + 1) + t, pivot = -1;
for (int l = 0; l < reduced.size(); ++l) if (reduced[l][id] != 0) {
pivot = l;
break;
}
if (pivot == -1) continue;
vector<Z> row = reduced[pivot];
Z in = row[id].inv();
for (auto &a : reduced) if (&a != &reduced[pivot] && a[id] != 0) {
Z z = a[id] * in;
for (int l = 0; l < cols; ++l) a[l] -= z * row[l];
}
reduced.erase(reduced.begin() + pivot);
}
bool found = false;
for (auto &a : reduced) if (valid(a)) {
ans = a;
found = true;
break;
}
if (!found) break;
}
PRec ret(j + 1);
for (int k = 0; k <= j; ++k) {
for (int t = cap; t >= 0; --t) ret[k] = ret[k] * opoly{-Z(k), Z(1)} + ans[k * (cap + 1) + t];
ret[k].shrink();
}
if (keep) ret[0] = rec[0] * ans[0];
while (!ret.empty() && ret.back().empty()) ret.pop_back();
if (first.empty()) first = ret;
if (frontier && !keep) frontier->push_back(ret);
else return ret;
}
return first.empty() ? rec : first;
};
PRec short_rec = solve(d, m, false), safe = solve(d, m, true);
return {short_rec, safe};
}
vector<poly> best_recur(const PRecBudget &budget = PRecBudget());
vector<poly> best_recur_order(int m, const PRecBudget &budget = PRecBudget());
vector<poly> best_recur_after(u64 M, const PRecBudget &budget = PRecBudget());
PRec rawPRec() {
shrink();
if (degree() == 0 && ode[0].empty()) throw domain_error("zero differential operator");
int tmp = numeric_limits<int>::max();
int n = degree(), m = numeric_limits<int>::min();
for (int i = 0; i <= n; ++i) {
if (ode[i].empty()) continue;
m = max(m, (int) ode[i].size() - 1 - i);
int j = 0;
while (ode[i][j] == 0) ++j;
tmp = min(tmp, j - i);
} m -= tmp;
PRec rec(m + 1);
for (int i = 0; i <= n; ++i)
for (int j = 0; j < ode[i].size(); ++j) if (ode[i][j] != 0) {
int k = j - i - tmp;
opoly coef{Z(1)};
for (int t = 0; t < i; ++t) coef = coef * opoly{-Z(k + t), Z(1)};
rec[k] = rec[k] + coef * ode[i][j];
}
for (int i = 0; i <= m; ++i) rec[i].shrink();
return rec;
}
inline PRec getPRec() {
PRec raw = rawPRec();
_frontier.clear();
tie(_rec, _safe) = reduce(raw, -1, &_frontier);
auto key = [&](const PRec &rec) {
auto [m, d] = recur_shape(rec);
return make_tuple(recur_cost(m, d), m, d);
};
for (auto &rec : _frontier) if (key(rec) < key(_rec)) _rec = rec;
if (key(_safe) < key(_rec)) _rec = _safe;
_source = ode;
return _rec;
}
vector<PRec> frontier(int d = -1) {
vector<PRec> ret;
if (d == -1) {
if (_rec.empty() || _source != ode) getPRec();
ret = _frontier;
} else reduce(rawPRec(), d, &ret);
auto deg = [&](const PRec &a) {
int v = 0;
for (auto &p : a) v = max(v, p.degree());
return v;
};
sort(ret.begin(), ret.end(), [&](const PRec &a, const PRec &b) {
return a.size() != b.size() ? a.size() < b.size() : deg(a) < deg(b);
});
vector<PRec> ans;
int best = numeric_limits<int>::max();
for (auto &a : ret) if (deg(a) < best) best = deg(a), ans.push_back(a);
return ans;
}
int prf(int n) {
if (_rec.empty() || _source != ode) getPRec();
coef.resize(n + 1);
for (int i = 0; i <= n; ++i)
coef[i] = _rec[0].eval(i);
int r = 0;
for (int i = n; i; --i)
if (coef[i] == 0) {
r = i;
break;
}
return r;
}
opoly post(opoly init) {
if (_rec.empty() || _source != ode) getPRec();
int m = init.size();
if (coef.size() < m) throw domain_error("unprepared initial extension");
if (coef.size() == m) return init;
for (int i = m; i < coef.size(); ++i) if (coef[i] == 0) throw domain_error("insufficient singular initial values");
auto invs = [&](const opoly &vec) {
opoly prf(vec.size()), ret(vec.size());
prf[0] = 1;
for (int i = 1; i < vec.size(); ++i) prf[i] = prf[i - 1] * (vec[i - 1] == 0 ? Z(1) : vec[i - 1]);
// Z tot = accumulate(vec.begin(), vec.end(), Z(1), multiplies<Z>()).inv();
// for (int i = (int) vec.size() - 1; i >= 0; --i) ret[i] = tot * prf[i], tot *= vec[i];
Z tot = Z(1);
for (auto v : vec) if (v.v != 0) tot *= v;
tot = tot.inv();
for (int i = (int) vec.size() - 1; i >= 0; --i)
if (vec[i].v != 0) ret[i] = tot * prf[i], tot *= vec[i];
return ret;
};
auto nvs = invs(vector<Z>(coef.begin() + m, coef.end()));
init.resize(coef.size());
for (int i = m; i < coef.size(); ++i) {
for (int j = 1; j < min(i + 1, (int) _rec.size()); ++j)
init[i] += init[i - j] * _rec[j].eval(i);
init[i] = init[i] * -nvs[i - m];
} return init;
}
poly recur(const int& n, const poly& a0) {
if (_rec.empty() || _source != ode) getPRec();
swap(_rec, _safe);
int last = prf(n);
if (last >= a0.size()) {
swap(_rec, _safe);
throw domain_error("insufficient singular initial values");
}
opoly ret(a0.size());
for (int i = 0; i < a0.size(); ++ i) ret[i].v = a0[i];
if (n <= ret.degree()) ret.redegree(n);
else ret = post(ret);
swap(_rec, _safe);
return ret;
}
vector<poly> trans_poly() {
if (_rec.empty() || _source != ode) getPRec();
vector<poly> ret, safe;
for (auto &p : _rec) ret.push_back(p.operator poly());
for (auto &p : _safe) safe.push_back(p.operator poly());
_PR_base::fallback[_PR_base::key(ret)] = move(safe);
return ret;
}
PRecData trans_data() {
if (_rec.empty() || _source != ode) getPRec();
PRecData ret;
for (auto &a : _rec) ret.first.push_back(a.operator poly());
for (auto &a : _safe) ret.second.push_back(a.operator poly());
auto insert = [&](vector<poly> a) {
u32 in = qp(a[0][a[0].degree()], mod - 2);
for (auto &p : a) p *= in;
for (auto &b : ret.candidates) if (a.size() == b.size()) {
bool same = true;
for (int i = 0; i < a.size(); ++i) if (a[i].f != b[i].f) same = false;
if (same) return;
}
ret.candidates.push_back(a);
};
insert(ret.second), insert(ret.first);
for (auto &rec : _frontier) {
vector<poly> a;
for (auto &p : rec) a.push_back(p.operator poly());
insert(a);
}
return ret;
}
inline friend ODE operator+(const ODE &op, const ODE &oq) {
int n = op.degree(), m = oq.degree();
if ((n == 0 && op[0].empty()) || (m == 0 && oq[0].empty())) throw domain_error("zero differential operator");
if (n == 0) return oq;
if (m == 0) return op;
Q_Basis basis(n + m);
vector<pfrac> pd(n + 1), qd(m + 1);
pd[0] = qd[0] = pfrac({Z(1)});
for (int dim = 0; dim <= n + m; ++dim) {
vector<pfrac> vec(n + m);
copy(pd.begin(), pd.begin() + n, vec.begin());
copy(qd.begin(), qd.begin() + m, vec.begin() + n);
auto ret = basis.insert(vec);
if (!ret.empty()) {
ret.resize(dim + 1);
return from_frac(ret);
} pd[n] = qd[m] = pfrac();
for (int j = n - 1; j >= 0; --j) pd[j + 1] = pd[j + 1] + pd[j], pd[j] = pd[j].deri();
for (int j = m - 1; j >= 0; --j) qd[j + 1] = qd[j + 1] + qd[j], qd[j] = qd[j].deri();
for (int j = 0; j < n; ++j) pd[j] = pd[j] - pd[n] * op[j] / op[n];
for (int j = 0; j < m; ++j) qd[j] = qd[j] - qd[m] * oq[j] / oq[m];
}
}
inline friend ODE operator*(const ODE &op, const ODE &oq) {
int n = op.size() - 1, m = oq.size() - 1;
if ((n == 0 && op[0].empty()) || (m == 0 && oq[0].empty())) throw domain_error("zero differential operator");
if (n == 0 || m == 0) return ODE{{Z(1)}};
if (m == 1 && n != 1) return oq * op;
if (n == 1) {
pfrac h(op[0], op[1]);
vector<pfrac> v(m + 1), w(m + 1);
w[0] = pfrac({Z(1)});
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= i; ++j) v[j] = v[j] + w[j] * pfrac(oq[i]);
if (i == m) break;
for (int j = i + 1; j >= 0; --j) {
w[j] = w[j].deri() + h * w[j];
if (j) w[j] = w[j] + w[j - 1];
}
}
return from_frac(v);
}
Q_Basis basis(n * m);
vector<vector<pfrac> > p(n + 1, vector<pfrac>(m + 1));
p[0][0] = pfrac({Z(1)});
for (int dim = 0; dim <= n * m; ++dim) {
vector<pfrac> vec(n * m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
vec[i * m + j] = p[i][j];
auto ret = basis.insert(vec);
if (!ret.empty()) {
ret.resize(dim + 1);
return from_frac(ret);
}
for (int i = 0; i < n; ++i) p[i][m] = pfrac();
for (int j = 0; j < m; ++j) p[n][j] = pfrac();
for (int i = n - 1; i >= 0; --i)
for (int j = m - 1; j >= 0; --j) {
p[i + 1][j] = p[i + 1][j] + p[i][j];
p[i][j + 1] = p[i][j + 1] + p[i][j];
p[i][j] = p[i][j].deri();
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
p[i][j] = p[i][j] - p[n][j] * op[i] / op[n] - p[i][m] * oq[j] / oq[m];
}
}
inline ODE composite(pfrac q) const {
q.shrink();
int n = ode.size() - 1;
if (n == 0) {
if (ode[0].empty()) throw domain_error("zero differential operator");
return *this;
}
vector<vector<pfrac>> tri(n + 1, vector<pfrac>(n + 1));
tri[0][0] = pfrac({Z(1)});
pfrac d = q.deri();
if (d.x.empty()) return ODE{{Z()}, {Z(1)}};
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i; ++j) {
tri[i + 1][j] = tri[i + 1][j] + tri[i][j].deri();
tri[i + 1][j + 1] = tri[i][j] * d;
}
} vector<pfrac> vec(n + 1);
int deg = 0;
for (auto &a : ode) deg = max(deg, a.degree());
vector<opoly> pw(deg + 1, opoly{Z(1)});
for (int i = 1; i <= deg; ++i) pw[i] = pw[i - 1] * q.y;
for (int i = 0; i <= n; ++i) {
opoly a;
for (int j = ode[i].degree(); j >= 0; --j) a = a * q.x + pw[ode[i].degree() - j] * ode[i][j];
if (!ode[i].empty()) vec[i] = pfrac(a, pw[ode[i].degree()]);
}
for (int i = n; i >= 0; --i) {
vec[i] = vec[i] / tri[i][i];
for (int j = 0; j < i; ++j) vec[j] = vec[j] - vec[i] * tri[i][j];
}
return from_frac(vec);
}
};
inline int p_recur(const u64& n, const poly& a, const PRecData &P) {
if (n <= a.degree()) return a[n];
int ans = _PR_base::p_recur(n, a, P.first);
if (ans == -1) ans = _PR_base::p_recur(n, a, P.second);
if (ans == -1) throw domain_error("singular recurrence interval");
return ans;
}
ODE linear_add(ODE a, ODE b) {
if (a.size() < b.size()) swap(a, b);
for (int i = 0; i < b.size(); ++i) a[i] = a[i] + b[i];
return a;
}
ODE scalar_mul(ODE ode, const Z &z) {
for (int i = 0; i < ode.size(); ++i)
ode[i] = ode[i] * z;
return ode;
}
ostream &operator<<(ostream &out, const opoly &p) {
if (p.empty()) return out << 0;
out << p[0]; for (int i = 1; i < p.size(); ++i)
out << " + " << p[i] << "x^" << i;
return out;
}
ostream &operator<<(ostream &out, const pfrac &q) { return out << '(' << q.x << ") / (" << q.y << ')'; }
ostream &operator<<(ostream &out, const ODE &ode) {
out << "(" << ode[0] << ")F";
for (int i = 1; i < ode.size(); ++i) {
out << " + (" << ode[i] << ")F^{(" << i << ")}";
} return out << " = 0";
}
template<class T> ostream &operator<<(ostream &out, const vector<T> &v) {
if (!v.empty()) {
out << v.front();
for (int i = 1; i < v.size(); ++i) out << ' ' << v[i];
} return out;
}
ostream &operator>>(ostream &in, const Z &q) { return in >> q.v; }
template<class T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
const ODE ODE_EXP = {{-Z(1)}, {Z(1)} };
const ODE ODE_LN = {{Z()}, {-Z(1)}, {Z(1), -Z(1)} };
ODE ode_power(const Z &k) { return ODE{{-Z(k)}, {Z(), Z(1)}}; }
ODE ode_pfrac(const pfrac &a) { return ode_power(1).composite(a); }
ODE ode_pFq(const vector<Z> &a0, const vector<Z> &b0) {
vector<Z> a = a0, b = b0;
for (int i = 0; i < a.size(); ) {
auto it = find(b.begin(), b.end(), a[i]);
if (it == b.end()) ++i;
else a.erase(a.begin() + i), b.erase(it);
}
ODE l = {{Z(1)}}, r = l;
for (int x : a) l = linear_add(l.theta(), scalar_mul(l, x));
for (int x : b) r = linear_add(r.theta(), scalar_mul(r, x - 1));
ODE ret = linear_add(r.deri(), scalar_mul(l, - Z(1)));
return ret.shrink(), ret;
}
化简某个类可以使用成员函数 shrink()。
struct Z:用来维护模 \(\text{mod}\) 意义下一个数的类,使用 struct FastMod 进行底层乘法优化。
struct opoly:继承了类 vector<Z>,用来维护短多项式。由于模数可能不是多项式模数,底层使用 \(O(n^2)\) 多项式乘法。封装了基础多项式运算 \(+-\times /\)、成员函数求导 deri() 和求值 eval(int x),以及(朴素的)多项式欧几里得 gcd(opoly, opoly)。
struct pfrac:用来维护有理分式,分子与分母均为 opoly 类,分别命名为 x 和 y。封装了基础运算 \(+-\times /\)、成员函数求导 deri()。
struct Q_Basis:用来维护类似线性基的结构,便于 ODE 的加法与乘法的进行。
struct ODE:大的来了!这个是三合一,可以通过调用成员函数实现:ODE 的 \(+\times \circ\),以及由 ODE 求解整式递推系数多项式、还有线性递推出前 \(n\) 项系数。一个个说。
上面提到的 ODE 的加法、乘法可以直接用运算符进行,而复合需要调用成员函数 composite(pfrac),注意这里只能复合有理分式。
由 ODE 求解整式递推,需要调用 getPRec(),得到的整式递推会被存在 ODE 类里的 _rec(这是个 vector<opoly>)中,其存放方式与【模板】整式递推中相同。现在返回已搜索候选中权值最小者:\(m\) 阶 \(d\) 次递推(\(d\ge1\))的权值固定为 \(m^2\sqrt d(m+15+\tfrac12\log_2d)\),常系数另计。但如果我们想调用 \(O(\sqrt n \log n)\) 自动机,则需要将其转为 vector<poly>,使用成员函数 trans_poly() 接收返回值即可。与 \(O(\sqrt n \log n)\) 自动机的配合只需要把需要的值求出来即可。
线性递推出前 \(n\) 项系数,使用 recur(const int& n, const poly& a0),其中 a0 存放初值,其返回 \(a_0, \dots, a_n\),并存放在一个 poly 中返回。
upd. gpt(部分)解决了奇点问题,下面是几个许愿得到的函数。另有 best_recur() 求最小权值,best_recur_order(m) 限制阶数不超过 \(m\)、先最大化最低零点再最小化权值,best_recur_after(M) 要求最低零点大于 \(M\)、再最小化权值。零点指递推首项系数的最低正整数零点,零剩余类记为 \(p\),无根记为 u64 最大值。三者均返回 vector<poly>,可直接传给整式递推自动机。可追加 PRecBudget 调整搜索预算;超时、范围内无解、已证不可达分别抛出异常。搜索限于原 ODE 左倍式。
一些基础函数:
const ODE ODE_EXP 对应的是 \(\exp x\) 的 ODE;
const ODE ODE_LN 对应的是 \(-\ln (1-x)\) 的 ODE;
ODE ode_power(int k) 对应的是 \(x^k\) 的 ODE;
ODE ode_pFq(const vector<Z> &a, const vector<Z> &b) 对应的是 \(F\left(\left.\begin{matrix} a_1,\dots, a_p \\ b_1, \dots, b_q \end{matrix}\right\rvert x\right)\) 的 ODE;
注意:本自动机没有经过压力测试,可能出现不可预测的错误。
以下是博客签名,与正文无关。
请按如下方式引用此页:
本文作者 joke3579,原文链接:https://www.cnblogs.com/joke3579/p/-/ode-automaton。
遵循 CC BY-NC-SA 4.0 协议。
请读者尽量不要在评论区发布与博客内文完全无关的评论,视情况可能删除。

浙公网安备 33010602011771号