[THUWC 2017]在美妙的数学王国中畅游

本文介绍了解决BZOJ5020问题的方法,通过使用Taylor展开式来计算路径上函数的值,确保了答案误差小于10^-7。文中详细解释了如何利用12阶导数来逼近函数,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bzoj5020

107

题解

Taylor展开式:

f(x)n[a,b]f(x)x0[a,b]

f(x)=i=0nf(n)(x0)(xx0)ii!+Θ((xx0)n)

f(n)fnΘ((xx0)n)

x0=012107

用Taylor展开式可以直接把路径上的函数合并
直接开12变量个记录每一阶的导数,用LCT维护,统计答案用Taylor展开式计算

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(400010);
const double E = pow(2, 1.0 / log(2));

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m;

namespace LCT{
    int ch[2][_], fa[_], rev[_], S[_];
    double w[17][_], sum[17][_];

    IL bool Son(RG int x){  return ch[1][fa[x]] == x;  }

    IL bool Isroot(RG int x){  return ch[0][fa[x]] != x && ch[1][fa[x]] != x;  }

    IL void Update(RG int x){  for(RG int i = 0; i < 16; ++i) sum[i][x] = sum[i][ch[0][x]] + sum[i][ch[1][x]] + w[i][x];  }

    IL void Pushdown(RG int x){  if(!rev[x]) return; rev[x] ^= 1; rev[ch[0][x]] ^= 1; rev[ch[1][x]] ^= 1; swap(ch[0][x], ch[1][x]);  }

    IL void Rot(RG int x){
        RG int y = fa[x], z = fa[y], c = Son(x);
        if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
        ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y;
        ch[!c][x] = y; fa[y] = x; Update(y);
    }

    IL void Splay(RG int x){
        RG int top = 0; S[++top] = x;
        for(RG int i = x; !Isroot(i); i = fa[i]) S[++top] = fa[i];
        while(top) Pushdown(S[top--]);
        for(RG int y = fa[x]; !Isroot(x); Rot(x), y = fa[x])
            if(!Isroot(y)) Son(x) ^ Son(y) ? Rot(x) : Rot(y);
        Update(x);
    }

    IL void Access(RG int x){  for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), ch[1][x] = y, Update(x);  }

    IL int Findroot(RG int x){  Access(x); Splay(x); while(ch[0][x]) x = ch[0][x]; return x;  }

    IL void Makeroot(RG int x){  Access(x); Splay(x); rev[x] ^= 1;  }

    IL void Split(RG int x, RG int y){  Makeroot(x); Access(y); Splay(y);  }

    IL void Link(RG int x, RG int y){  Makeroot(x); fa[x] = y;  }

    IL void Cut(RG int x, RG int y){  Split(x, y); ch[0][y] = fa[x] = 0;  }

    IL double Query(RG int x, RG int y, RG double xx){
        Split(x, y);
        RG double ans = sum[0][y], fac = 1, xxx = 1;
        for(RG int i = 1; i < 16; i++) fac *= i, xxx *= xx, ans += sum[i][y] / fac * xxx;
        return ans;
    }

    IL void Calc(RG int x, RG int f, RG double a, RG double b){
        for(RG int i = 0; i < 16; ++i) w[i][x] = 0;
        if(f == 3) w[0][x] = b, w[1][x] = a;
        else if(f == 1){
            RG double aa = 1; w[0][x] = sin(b);
            for(RG int i = 1; i < 16; ++i){
                aa *= a;
                if(i % 4 == 1) w[i][x] = aa * cos(b);
                else if(i % 4 == 2) w[i][x] = -aa * sin(b);
                else if(i % 4 == 3) w[i][x] = -aa * cos(b);
                else w[i][x] = aa * sin(b);
            }
        }
        else{
            w[0][x] = pow(E, b);
            for(RG int i = 1; i < 16; ++i) w[i][x] = w[i - 1][x] * a;
        }
    }

    IL void Work(){
        RG char c; RG int u, v, p, f; RG double x, a, b;
        for(RG int i = 1; i <= n; ++i) f = Read(), scanf("%lf%lf", &a, &b), Calc(i, f, a, b);
        while(m--){
            scanf(" %c", &c);
            if(c == 'a') u = Read() + 1, v = Read() + 1, Link(u, v);
            else if(c == 'd') u = Read() + 1, v = Read() + 1, Cut(u, v);
            else if(c == 'm') p = Read() + 1, f = Read(), scanf("%lf%lf", &a, &b), Calc(p, f, a, b), Splay(p);
            else{
                u = Read() + 1; v = Read() + 1; scanf("%lf", &x);
                if(Findroot(u) != Findroot(v)) puts("unreachable");
                else printf("%.8e\n", Query(u, v, x));
            }
        }
    }
}

int main(RG int argc, RG char *argv[]){
    n = Read(); m = Read(); Read();
    LCT::Work();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值