洛谷 P8989 [北大集訓 2021] 隨機遊走 題解

XuYueming發表於2024-04-25

前言

又是隨機遊走?

題目分析

看到加邊,可能性太多了。但是為了讓步數最大化,我們可以貪心地想,肯定要往前面連,而且越前面要走的期望步數肯定越大。並且,我們不會浪費邊在終點上。於是,題目轉變成了 \(1 \sim n - 1\) 連向起點 \(1\) 連若干條邊,使得隨機遊走到終點的期望步數最大。

那要如何分配這 \(m\) 條邊到 \(1 \sim n - 1\) 個點呢?考慮假設已知第 \(i\) 個點向 \(1\) 連了 \(d_i\) 條邊,求期望步數。設 \(f_i\) 為到了 \(i\),還要期望多少步走到終點,顯然 \(f_n = 0\)。開始喜聞樂見的推式子環節:

\[\large f_i = \cfrac{1}{d_i + 1}f_{i+1} + \cfrac{d_i}{d_i + 1}f_1+1 \]

\(n-1\) 向前遞推。

\[\begin{aligned} \large f_{n-1} &= \cfrac{1}{d_{n-1} + 1}f_{n-1+1} + \cfrac{d_{n-1}}{d_{n-1} + 1}f_1+1 \\ &= \cfrac{d_{n-1}}{d_{n-1} + 1}f_1+1 \end{aligned} \]

推到 \(n-2\)

\[\begin{aligned} \large f_{n-2} &= \cfrac{1}{d_{n-2} + 1}f_{n-1} + \cfrac{d_{n-2}}{d_{n-2} + 1}f_1 + 1 \\ &= \cfrac{1}{d_{n-2} + 1} \cdot \left(\cfrac{d_{n-1}}{d_{n-1} + 1}f_1 + 1\right) + \cfrac{d_{n-2}}{d_{n-2} + 1}f_1 + 1\\ &= \cfrac{1}{d_{n-2} + 1} \cdot \cfrac{d_{n-1}}{d_{n-1} + 1}f_1 + \cfrac{d_{n-2}}{d_{n-2} + 1}f_1 + \cfrac{1}{d_{n-2} + 1} + 1 \\ &= \cfrac{d_{n-1} + d_{n-2} \cdot (d_{n-1}+1)}{(d_{n-2} + 1) \cdot (d_{n-1} + 1)} f_1 + \cfrac{(d_{n - 2} + 1) + 1}{d_{n-2} + 1} \\ &= \cfrac{(d_{n-2} + 1) \cdot (d_{n-1}+1) - 1}{(d_{n-2} + 1) \cdot (d_{n-1} + 1)} f_1 + \cfrac{(d_{n - 2} + 1) + 1}{d_{n-2} + 1} \end{aligned} \]

再推到 \(n-3\)

\[\begin{aligned} f_{n-3} =& \cfrac{1}{d_{n-3} + 1}f_{n-2} + \cfrac{d_{n-3}}{d_{n-3} + 1}f_1+1 \\ =& {\scriptsize \cfrac{1}{d_{n-3} + 1}\left(\cfrac{(d_{n-2} + 1) \cdot (d_{n-1}+1) - 1}{(d_{n-2} + 1) \cdot (d_{n-1} + 1)} f_1 + \cfrac{(d_{n - 2} + 1) + 1}{d_{n-2} + 1}\right) + \cfrac{d_{n-3}}{d_{n-3} + 1}f_1+1} \\ =& {\scriptsize \cfrac{(d_{n-2} + 1) \cdot (d_{n-1}+1) - 1}{(d_{n-3} + 1) \cdot (d_{n-2} + 1) \cdot (d_{n-1} + 1)} f_1 + \cfrac{(d_{n - 2} + 1) + 1}{(d_{n-3} + 1) \cdot (d_{n-2} + 1)} + \cfrac{d_{n-3}}{d_{n-3} + 1}f_1+1} \\ =& {\scriptsize \cfrac{d_{n-3} \cdot (d_{n-2} + 1) \cdot (d_{n-1} + 1) + (d_{n-2} + 1) \cdot (d_{n-1}+1) - 1}{(d_{n-3} + 1) \cdot (d_{n-2} + 1) \cdot (d_{n-1} + 1)} f_1 + } \\ & {\scriptsize \cfrac{(d_{n-3} + 1) \cdot (d_{n-2}+1) + (d_{n - 2} + 1) + 1}{(d_{n-3} + 1) \cdot (d_{n-2}+1)}} \\ =& {\scriptsize \cfrac{(d_{n-3} + 1) \cdot (d_{n-2} + 1) \cdot (d_{n-1} + 1) - 1}{(d_{n-3} + 1) \cdot (d_{n-2} + 1) \cdot (d_{n-1} + 1)} f_1 + \cfrac{(d_{n-3} + 1) \cdot (d_{n-2}+1) + (d_{n - 2} + 1) + 1}{(d_{n-3} + 1) \cdot (d_{n-2}+1)}} \end{aligned} \]

找到一些規律,嘗試去證明。假設對於 \(i+1\) 滿足:

\[ \large f_{i+1} = \cfrac{\prod \limits _ {j=i+1}^{n-1} (d_j+1)-1}{\prod \limits _ {j=i+1}^{n-1} (d_j+1)}f_1 + \cfrac{\sum \limits _ {j=i+1} ^ {n-2} \prod \limits _ {k=j} ^ {n-2}(d_k+1) + 1}{\prod \limits _ {j=i+1} ^ {n-2} (d_j+1)} \]

顯然該式對於 \(n-1\) 成立。嘗試用歸納法推到 \(i\)

\[ \begin{aligned} f_i &= {\scriptsize \cfrac{1}{d_i + 1}\left(\cfrac{\prod \limits _ {j=i+1}^{n-1} (d_j+1)-1}{\prod \limits _ {j=i+1}^{n-1} (d_j+1)}f_1 + \cfrac{\sum \limits _ {j=i+1} ^ {n-2} \prod \limits _ {k=j} ^ {n-2}(d_k+1) + 1}{\prod \limits _ {j=i+1} ^ {n-2} (d_j+1)}\right) + \cfrac{d_i}{d_i + 1}f_1+1 } \\ &= {\scriptsize \cfrac{\prod \limits _ {j=i+1}^{n-1} (d_j+1)-1}{\prod \limits _ {j=i}^{n-1} (d_j+1)}f_1 + \cfrac{\sum \limits _ {j=i+1} ^ {n-2} \prod \limits _ {k=j} ^ {n-2}(d_k+1) + 1}{\prod \limits _ {j=i} ^ {n-2} (d_j+1)} + \cfrac{d_i}{d_i + 1}f_1+1} \\ &= {\scriptsize \cfrac{\prod \limits _ {j=i}^{n-1} (d_j+1)-1}{\prod \limits _ {j=i}^{n-1} (d_j+1)}f_1 + \cfrac{\sum \limits _ {j=i} ^ {n-2} \prod \limits _ {k=j} ^ {n-2}(d_k+1) + 1}{\prod \limits _ {j=i} ^ {n-2} (d_j+1)}} \end{aligned} \]

所以上式對於所有 \(i\) 均成立。考慮邊界,推到 \(i=1\) 的時候是一個方程。

\[ f_1 = \cfrac{\prod \limits _ {j=1}^{n-1} (d_j+1)-1}{\prod \limits _ {j=1}^{n-1} (d_j+1)}f_1 + \cfrac{\sum \limits _ {j=1} ^ {n-2} \prod \limits _ {k=j} ^ {n-2}(d_k+1) + 1}{\prod \limits _ {j=1} ^ {n-2} (d_j+1)} \]

解方程。

\[ {\scriptsize \left( \prod \limits _ {j=1}^{n-1} (d_j+1) \right)f_1 = \left (\prod \limits _ {j=1}^{n-1} (d_j+1)-1\right)f_1 + (d_{n-1} + 1)\left({\sum \limits _ {j=1} ^ {n-2} \prod \limits _ {k=j} ^ {n-2}(d_k+1) + 1}\right)} \]

\[ f_1 = {\sum \limits _ {j=1} ^ {n-2} \prod \limits _ {k=j} ^ {n-1}(d_k+1) + (d_{n-1}+1)} \]

\[ \large f_1 = \sum \limits _ {j=1} ^ {n-1} \prod \limits _ {k=j} ^ {n-1}(d_k+1) \]

於是我們可以很方便地求出期望步數,即 \(f_1\)。但是我們還是不知道最優的 \(d\) 如何分配,考慮打表找規律。在此之前,我們不妨試著找一找 \(d\) 的性質。

首先,\(d\) 一定是單調不降的。因為顯然,放在後面會給更多的 \(\prod\) 提供貢獻,從而使 \(f_1\) 更大。

其次……沒其次了,打表吧。

while True:
    n, m = map(int, input().split())

    res = [0] * n
    ans = [0] * n

    maxx = 0

    def dfs(now, tot, x):
        global maxx, ans
        if now == n - 1:
            res[n - 1] = tot
            
            tmp = 0
            mul = 1
            for i in range(n - 1, 0, -1):
                mul *= res[i] + 1
                tmp += mul
            
            if tmp > maxx:
                maxx = tmp
                ans = res[::]
            
            return
        i = x
        while i * (n - now) <= tot:
            res[now] = i
            dfs(now + 1, tot - i, i)
            i += 1

    dfs(1, m, 0)

    print(' '.join(map(str, ans[1:])))

以上是打表程式。考慮 \(n=20\) 時,不斷加邊對最優 \(d\) 的分配造成的影響。

m = 1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
m = 2: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
......
m = 18: 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
m = 19: 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
m = 20: 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2
......
m = 35: 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
m = 36: 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
m = 37: 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
m = 38: 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
......
m = 54: 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
m = 55: 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
m = 56: 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
m = 57: 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4
......

發現,當 \(m < n - 1\) 時,加邊是從 \(n-1\) 開始放到 \(2\),然後當 \(m \geq n - 1\) 時,從 \(n-1\) 開始放到 \(1\),如此往復。感性理解,具體證明交給讀者。

那麼,我們分別考慮兩種情況即可。

情況一

\(m < n - 1\) 時,邊集中在 \(n - m \sim n - 1\),其他位置 \(d\) 均為 \(0\)。此時有 $f_1 = $。

相關文章