题解:P13350「ZYZ 2025」遗传

· · 题解

「ZYZ 2025」遗传 【题解】

感觉可以称作灵活运用条件概率的典题。

先根据问题建出图,从父母向子代建一条有向边,发现最终图会成为一棵二叉内向树,为了遍历树更方便,不妨从子代向父母建边,建成外向树。对于显性疾病,把有病的当作没病,没病的当作有病后等价于隐性疾病,因此只考虑隐性疾病即可。

如果从子代向父母倒推每个生物的基因型可能是什么是极其困难的,那不妨从整体角度考虑这个问题。把 k 数组对生物患病情况的限制看作一个大条件,那么这个问题就是一个条件概率,记该条件为事件 B。现在记第 i 个生物基因型为 AA,Aa,aa 为事件 AA_i,Aa_i,aa_i。那么则需对每个 i,求出 \operatorname{P}(AA_i|B),\operatorname{P}(Aa_i|B),\operatorname{P}(aa_i|B) 考虑 \operatorname{P}(AA_i|B) 怎么求,其余同理。

\operatorname{P}(AA_i|B) = \dfrac{\operatorname{P}(AA_i\times B)}{\operatorname{P}(B)}

分母中的 B 事件可用更形象,更易理解的语言概括,把建出的外向树的每个节点染一下色,患病染为黑色,不患病染为白色,未知状态可以染为任意颜色(不限制),那么 B 事件就是“随机一个图,其颜色和数据给出的颜色一样的概率”。可以设 dp_{u,AA/Aa/aa} 为:u 子树内颜色满足限制,且第 u 个生物为 AA/Aa/aa 的概率。由于图是内向树,fa_u 和 mo_u 没有公共祖先,所以它们的基因型是什么互为独立事件,可直接使用乘法原理和加法原理计算 dp_{u,AA/Aa/aa} 的值。

其余同理,在回溯时计算即可。计算 \operatorname{P}(AA_i\times B) 的值也是相同的方法,钦定第 i 个生物的基因型只为 AA 就行,dp_{i,Aa},dp_{i,aa} 设成零。

预处理出 2,4 的逆元,条件概率的分母只用算一次,对于每个 i 用 dp 跑分子的时候只更新从 i 到根的那条链,由于数据保证随机生成,总时间复杂度均摊 O(n\log{n})。

代码:

#include <bits/stdc++.h>
// Modular 模板,可跳过到 67 行。 
template <typename T, const T MOD, typename MultiplicationType>
class Modular {
private:
    T value;
    T exgcd(const T& a, const T& b, T& x, T& y) const {
        if (b == 0) { x = 1, y = 0; return a; }
        T g = exgcd(b, a % b, y, x);
        y -= (MultiplicationType) (a / b) * x % MOD;
        if (y < 0) y += MOD;
        if (y >= MOD) y -= MOD;
        return g;
    }
public:
    Modular(const T& v = 0) : value(v % MOD) { if (value < 0) value += MOD; }
    Modular(const Modular& other) : value(other.value) {}
    Modular& operator=(const Modular& other) { value = other.value; return *this; }
    Modular& operator=(const T& v) { value = v % MOD; if (value < 0) value += MOD; return *this; }
    operator int() const { return static_cast<int>(value); }
    operator long long() const { return static_cast<long long>(value); }
    operator double() const { return static_cast<double>(value); }
    operator float() const { return static_cast<float>(value); }
    operator bool() const { return value != 0; }
    T getVal() const { return value; }
    void setVal(const T& v) { value = v; }
    Modular operator+(const Modular& other) const { T result = value + other.value; if (result >= MOD) { result -= MOD; } return Modular(result); }
    Modular operator-(const Modular& other) const { T result = value - other.value; if (result < 0) { result += MOD; } return Modular(result); }
    Modular operator*(const Modular& other) const { return Modular((MultiplicationType) value * other.value % MOD); }
    Modular& operator+=(const Modular& other) { value += other.value; if (value >= MOD) { value -= MOD; } return *this; }
    Modular& operator-=(const Modular& other) { value -= other.value; if (value < 0) { value += MOD; } return *this; }
    Modular& operator*=(const Modular& other) { value = (MultiplicationType) value * other.value % MOD; return *this; }
    template<typename __Ty>
    Modular fPow(__Ty exp) const {
        Modular result(1), base(value);
        while (exp) {
            if (exp & 1) result *= base;
            base *= base; exp >>= 1;
        }
        return result;
    }
    template<typename __Ty>
    Modular& fPowSelf(__Ty exp) {
        Modular result(1), base(value);
        while (exp) {
            if (exp & 1) result *= base;
            base *= base; exp >>= 1;
        }
        return *this = result;
    }
    Modular inv() const {
        T x, y;
        assert(value);
        T g = exgcd(value, MOD, x, y);
        assert(g == T(1));
        return Modular(x);
    }
    Modular operator/(const Modular& other) const { return *this * other.inv(); }
    Modular& operator/=(const Modular& other) { return *this *= other.inv(); }
    bool operator==(const Modular& other) const { return value == other.value; }
    bool operator!=(const Modular& other) const { return value != other.value; }
    friend std::ostream& operator << (std::ostream& os, const Modular& o) {
        os << o.getVal();
        return os;
    }
};
using namespace std;
typedef Modular<int, 1000000007, long long> Int;
const int N = 1e5 + 10;
#define aa 0
#define AA 1
#define Aa 2
#define ILL 0
#define FINE 1
#define UK 2
Int f[N][3], P[N][3], p, isAA, isAa, isaa;
int n, fa[N], mo[N], type[N], root, rd[N];
void init() {
    isAA = p * p;
    isAa = Int(2) * p * (Int(1) - p);
    isaa = (Int(1) - p) * (Int(1) - p);
}
const Int Div4 = Int(4).inv(), Div2 = Int(2).inv();
inline Int PAA(const Int Pf[N][3], const Int Pm[N][3], int u) {
    if (fa[u] == 0 && mo[u] == 0) return isAA;
    return Pf[fa[u]][AA] * Pm[mo[u]][AA] + (Pf[fa[u]][Aa] * Pm[mo[u]][AA] + Pf[fa[u]][AA] * Pm[mo[u]][Aa]) * Div2 + Pf[fa[u]][Aa] * Pm[mo[u]][Aa] * Div4;
}
inline Int PAa(const Int Pf[N][3], const Int Pm[N][3], int u) {
    if (fa[u] == 0 && mo[u] == 0) return isAa;
    return Pf[fa[u]][AA] * Pm[mo[u]][aa] + Pf[fa[u]][aa] * Pm[mo[u]][AA] + (Pf[fa[u]][Aa] * Pm[mo[u]][aa] + Pf[fa[u]][aa] * Pm[mo[u]][Aa] + Pf[fa[u]][Aa] * Pm[mo[u]][AA] + Pf[fa[u]][AA] * Pm[mo[u]][Aa] + Pf[fa[u]][Aa] * Pm[mo[u]][Aa]) * Div2;
}
inline Int Paa(const Int Pf[N][3], const Int Pm[N][3], int u) {
    if (fa[u] == 0 && mo[u] == 0) return isaa;
    return Pf[fa[u]][aa] * Pm[mo[u]][aa] + (Pf[fa[u]][Aa] * Pm[mo[u]][aa] + Pf[fa[u]][aa] * Pm[mo[u]][Aa]) * Div2 + Pf[fa[u]][Aa] * Pm[mo[u]][Aa] * Div4;
}
int son[N];
void precalc(const int u) {
    if (fa[u]) precalc(fa[u]);
    if (mo[u]) precalc(mo[u]);
    son[fa[u]] = u;
    son[mo[u]] = u;
    if (type[u] == UK) {
        f[u][AA] = PAA(f, f, u);
        f[u][Aa] = PAa(f, f, u);
        f[u][aa] = Paa(f, f, u);
    }else if (type[u] == ILL) {
        f[u][AA] = 0;
        f[u][Aa] = 0;
        f[u][aa] = Paa(f, f, u);
    } else {
        f[u][AA] = PAA(f, f, u);
        f[u][Aa] = PAa(f, f, u);
        f[u][aa] = 0;
    }
}
Int calc(int u, int x) {
    if (x == AA) {
        if (type[u] == UK || type[u] == FINE) P[u][AA] = PAA(f, f, u);
        else P[u][AA] = 0;
        P[u][Aa] = 0;
        P[u][aa] = 0;
    } else if (x == Aa) {
        P[u][AA] = 0;
        if (type[u] == UK || type[u] == FINE) P[u][Aa] = PAa(f, f, u);
        else P[u][Aa] = 0;
        P[u][aa] = 0;
    } else {
        P[u][AA] = 0;
        P[u][Aa] = 0;
        if (type[u] == UK || type[u] == ILL) P[u][aa] = Paa(f, f, u);
        else P[u][aa] = 0;
    }
    int last;
    while (son[u]) {
        last = u;
        u = son[u];
        if (type[u] == UK) {
            P[u][AA] = (fa[u] == last ? PAA(P, f, u) : PAA(f, P, u));
            P[u][Aa] = (fa[u] == last ? PAa(P, f, u) : PAa(f, P, u));
            P[u][aa] = (fa[u] == last ? Paa(P, f, u) : Paa(f, P, u));
        }else if (type[u] == ILL) {
            P[u][AA] = 0;
            P[u][Aa] = 0;
            P[u][aa] = (fa[u] == last ? Paa(P, f, u): Paa(f, P, u));
        } else {
            P[u][AA] = (fa[u] == last ? PAA(P, f, u): PAA(f, P, u));
            P[u][Aa] = (fa[u] == last ? PAa(P, f, u): PAa(f, P, u));
            P[u][aa] = 0;
        }
    }
    return P[root][AA] + P[root][Aa] + P[root][aa];
}
int main() {
    int a, b, t;
    cin >> n >> t >> a >> b;
    p = Int(a) / Int(b);
    init();
    for (int i = 1; i <= n; i++) {
        cin >> type[i] >> fa[i] >> mo[i];
        if (type[i] != UK) type[i] ^= t; 
        rd[fa[i]]++;
        rd[mo[i]]++;
    }
    for (int i = 1; i <= n; i++) {
        if (!rd[i]) {
            root = i;
        }
    }
    precalc(root);
    Int B = f[root][AA] + f[root][Aa] + f[root][aa], x, y, z;
    for (int i = 1; i <= n; i++) {
        if (type[i] == FINE) { // 计算部分即可推出剩下的部分,节省时间。 
            x = calc(i, AA) / B;
            y = Int(1) - x;
            z = 0;
        } else if (type[i] == ILL) {
            x = 0;
            y = 0;
            z = 1;
        } else {
            x = calc(i, AA) / B;
            y = calc(i, Aa) / B;
            z = Int(1) - x - y;
        }
        cout << x << ' ' << y << ' ' << z << '\n'; 
    }
    return 0;
}

哥哥,要进来吗 QAQ?