题解:P4986 逃离

· · 题解

题目传送门

\mathtt{My \ Blog} —— 题解:P4986 逃离

学过数学的都应该知道,可以把 这个图 中的黄色段平移至蓝色段,还可以把紫色段旋转到绿色段。

然后使用勾股定理。

设总时间为 T

图中蓝色水平段长为 A(x) \cdot T ,蓝色竖直段长为 B(x) \cdot T ,绿色段长为 C(x) \cdot T ,故列出方程:

A(x)^2 + B(x)^2 = C(x)^2

注意到这个 标签,我们令 f(x) = A(x)^2 + B(x)^2 - C(x)^2 然后用牛顿迭代法。

什么?你不会牛顿迭代? 牛顿迭代模板题

当然,这里再讲一下牛顿迭代。

牛顿迭代

我们首先 随机 一个最初的近似解。

(因为 x \in [L..R] ,所以可以先取 \displaystyle\frac{L + R}{2} )

假设当前近似解为 z ,画出点 (z, f(z)) 再此处的切线,设其与 x 的交点的横坐标为 z_0 ,那么这就是一个更优的近似解。然后重复这个迭代的过程。

根据导数的定义(即函数在某一点的导数就是该函数所代表的曲线在这一点上的切线斜率),所以过 (z, f(z)), (z_0, 0) 两点的直线的斜率为 f'(z) ,而根据初中数学,有可以得到这个斜率也为 \displaystyle\frac{f(z) - 0}{z - z_0} ,列出方程: \displaystyle f'(z) = \frac{f(z)}{z - z_0}

化简一下就可以得到 \displaystyle z_0 = z - \frac{f(z)}{f'(z)} ,这就是牛顿迭代的式子!

因为 f(x) = A(x)^2 + B(x)^2 - C(x)^2 ,所以 f'(x) = 2A(x)A'(x) + 2B(x)B'(x) - 2C(x)C'(x) 。

最后发现 $0 \le \text{La},\text{Lb},\text{Lc} \le 10^5$ ,所以 [![](https://cdn.luogu.com.cn/upload/image_hosting/s2p6bjaf.png)](/problem/list?tag=313) 。 一个小小的坑。题目最后一行:👇 ###### $\color{#EEE}{\tt {Notice\ that\ SPEED\ is\ VECTOR.(High\ school\ physics)}}

说明最后求解出来的解可以为负数!

Code:

#include <bits/stdc++.h>
using namespace std;‍‌
const int N = 1e6 + 10;‍‌
const double eps = 1e-10;‍‌
int la, lb, lc, a[N], b[N], c[N], d[N];‍‌
double L, R;‍‌
// FFT template
const double PI = acos(-1.0);‍‌
struct Comp{‍‌
    double x, y;‍‌
    Comp(double _x = 0.0, double _y = 0.0) {‍‌
        x = _x;‍‌
        y = _y;‍‌
    }‍‌
    Comp operator-(const Comp &a)const {‍‌
        return Comp(x - a.x, y - a.y);‍‌
    }‍‌
    Comp operator+(const Comp &a)const {‍‌
        return Comp(x + a.x, y + a.y);‍‌
    }‍‌
    Comp operator*(const Comp &a)const {‍‌
        return Comp(x * a.x - y * a.y, x * a.y + y * a.x);‍‌
    }‍‌
}‍‌tpa[N], tpb[N];‍‌
int n, m;‍‌
void change(Comp *y, int len) {‍‌
    int k;‍‌
    for(int i = 1, j = len / 2;‍‌ i < len - 1;‍‌ i ++) {‍‌
        if(i < j) swap(y[i], y[j]);‍‌
        k = len / 2;‍‌
        while(j >= k) {‍‌
            j -= k;‍‌
            k = k / 2;‍‌
        }‍‌
        if(j < k) j += k;‍‌
    }‍‌
}‍‌
void fft(Comp *y, int len, int on) {‍‌
    change(y, len);‍‌
    for(int h = 2;‍‌ h <= len;‍‌ h <<= 1) {‍‌
        Comp wn(cos(2 * PI / h), sin(on * 2 * PI / h));‍‌
        for(int j = 0;‍‌ j < len;‍‌ j += h) {‍‌
            Comp w(1, 0);‍‌
            for(int k = j;‍‌ k < j + h / 2;‍‌ k ++) {‍‌
                Comp u = y[k], t = w * y[k + h / 2];‍‌
                y[k] = u + t, y[k + h / 2] = u - t;‍‌
                w = w * wn;‍‌
            }‍‌
        }‍‌
    }‍‌
    if(on == -1) {‍‌
        for(int i = 0;‍‌ i < len;‍‌ i ++) y[i].x /= len;‍‌
        for(int i = 0;‍‌ i < len;‍‌ i ++) y[i].y /= len;‍‌
    }‍‌
}‍‌
int mul(Comp *x1, Comp *x2, int n, int m) {‍‌
    int len = 1;‍‌
    while(len < m + n + 5) len <<= 1;‍‌
    fft(x1, len, 1);‍‌
    fft(x2, len, 1);‍‌
    for(int i = 0;‍‌ i < len;‍‌ i ++) x1[i] = x1[i] * x2[i];‍‌
    fft(x1, len, -1);‍‌
    return len;‍‌
}‍‌
void sqr(int *x, int &n) {‍‌
    memset(tpa, 0, sizeof tpa);‍‌
    memset(tpb, 0, sizeof tpb);‍‌
    for(int i = 0;‍‌ i <= n;‍‌ i ++) tpa[i] = tpb[i] = (double)x[i];‍‌
    n = mul(tpa, tpb, n, n);‍‌
    for(int i = 0;‍‌ i <= n;‍‌ i ++) x[i] = (int)(tpa[i].x + 0.1);‍‌
}‍‌
double calc(double x) {‍‌
    double res = 0;‍‌
    for(int i = lc;‍‌ ~i;‍‌ i --) res = res * x + c[i];‍‌
    return res;‍‌
}‍‌
double calc_derivative(double x) {‍‌
    double res = 0;‍‌
    for(int i = lc - 1;‍‌ ~i;‍‌ i --) res = res * x + d[i];‍‌
    return res;‍‌
}‍‌
int cnt = 30;‍‌
void newton(double r) {‍‌
    double c;‍‌
    while(1) {‍‌
        c = calc(r);‍‌
        cnt --;‍‌
        if(fabs(c) < eps) break;‍‌
        r -= c / calc_derivative(r);‍‌
        r = max(r, L), r = min(r, R);‍‌
        if(!cnt) break;‍‌
    }‍‌
    if(cnt) printf("%.8lf\n", r);‍‌
    else printf("Inconsistent!\n");‍‌
}‍‌
int main()
{‍‌
    scanf("%d%d%d%lf%lf", &la, &lb, &lc, &L, &R);‍‌
    for(int i = 0;‍‌ i <= la;‍‌ i ++) scanf("%d", &a[i]);‍‌
    for(int i = 0;‍‌ i <= lb;‍‌ i ++) scanf("%d", &b[i]);‍‌
    for(int i = 0;‍‌ i <= lc;‍‌ i ++) scanf("%d", &c[i]);‍‌
    sqr(a, la), sqr(b, lb), sqr(c, lc);‍‌
    lc = max(lc, max(la, lb));‍‌
    for(int i = 0;‍‌ i <= lc;‍‌ i ++) c[i] = c[i] - (a[i] + b[i]);‍‌
    for(int i = 1;‍‌ i <= lc;‍‌ i ++) d[i - 1] = c[i] * i;‍‌
    newton((L + R) / 2);‍‌
    return 0;‍‌
}‍‌