#54945: TOP1 解題報告


321qwedsa000@gmail.com (灝)


// ZeroJudge f973 - /╲/\(•̀w•́)/\╱\
// 使用 C++ Template Metaprogramming (TMP) 展開 Weierstrass 級數
//
// f(x) = Σ_{n=0}^{∞} a^n cos(b^n π x)
//
// 數學分析:
//   ab >= 6 > 1 => Weierstrass 函數在任意點均不可微
//   切線斜率永遠不存在 => 永遠輸出 nan(有勝負時)
//
// TMP 架構:
//   WeierstrassTerm<N> 遞迴展開第 0..N 項(compile-time unrolling)
//   WeierstrassTerm<0> 偏特化終止遞迴
//   DepthPolicy<D>     控制展開深度的 Policy class
//   SeriesEvaluator<P> 組合 TMP 展開,對外提供 eval()

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

static constexpr double PI = 3.14159265358979323846;

// ─── TMP 遞迴展開:WeierstrassTerm<N> ─────────────────────────────────────
// 每一層展開計算 f 的一項並遞迴至下一層
// N = 還剩幾層;an = 目前的 a^idx;bn = 目前的 b^idx

template<int N>
struct WeierstrassTerm {
    static inline void accumulate(
        double a, int b, double an, double bn, double x, double& acc_f)
    {
        acc_f += an * std::cos(bn * PI * x);
        // TMP 遞迴:傳遞下一項的 a^(idx+1), b^(idx+1)
        WeierstrassTerm<N-1>::accumulate(a, b, an * a, bn * (double)b, x, acc_f);
    }
};

// 基底特化:終止 TMP 遞迴
template<>
struct WeierstrassTerm<0> {
    static inline void accumulate(
        double, int, double, double, double, double&) noexcept {}
};

// ─── Policy class:控制展開深度 ────────────────────────────────────────────
// a < 0.1 => a^20 < 1e-20,遠低於 double 精度需求
template<int D>
struct DepthPolicy { static constexpr int depth = D; };
using DefaultPolicy = DepthPolicy<20>;

// ─── 主求值器 ──────────────────────────────────────────────────────────────
template<typename Policy = DefaultPolicy>
struct SeriesEvaluator {
    [[nodiscard]]
    static double eval_f(double a, int b, double x) noexcept {
        double acc_f = 0.0;
        WeierstrassTerm<Policy::depth>::accumulate(a, b, 1.0, 1.0, x, acc_f);
        return acc_f;
    }
};

// ─── main ──────────────────────────────────────────────────────────────────
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    double a; int b;
    while (cin >> a >> b) {
        double x1, x2;
        cin >> x1 >> x2;

        // TMP 展開計算 f(x1), f(x2)
        const double f1 = SeriesEvaluator<>::eval_f(a, b, x1);
        const double f2 = SeriesEvaluator<>::eval_f(a, b, x2);
        const double diff = f1 - f2;

        if (std::fabs(diff) < 1.0) {
            cout << "Tie\n";
        } else {
            // 判斷勝負
            cout << (diff > 0.0 ? "Bully Maguire Wins\n" : "Zemo Wins\n");
            // ab >= 6 > 1 => Weierstrass 函數處處不可微 => 切線不存在 => nan
            cout << "nan\n";
        }
    }
    return 0;
}