<div class="blogpost-body cnblogs-markdown" id="cnblogs_post_body">
<div class="toc">
<p class="toc-title">目录</p>
<div class="toc-list">
<ul><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95%E4%B8%8E%E5%BF%AB%E9%80%9F%E5%B9%82">矩阵乘法与快速幂</a>
<ul><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95">矩阵乘法</a></li><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95%E7%9A%84%E7%BB%93%E5%90%88%E5%BE%8B">矩阵乘法的结合律</a></li><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#floyd-%E7%AE%97%E6%B3%95">Floyd 算法</a></li><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#%E5%BF%AB%E9%80%9F%E5%B9%82">快速幂</a></li><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95%E7%BB%93%E5%90%88%E5%BE%8B%E7%9A%84%E5%BA%94%E7%94%A8">矩阵乘法结合律的应用</a></li><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#%E4%BD%BF%E7%94%A8%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95%E5%BF%AB%E9%80%9F%E5%B9%82%E6%B1%82%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97">使用矩阵乘法(快速幂)求斐波那契数列</a></li><li><a href="https://blog.csdn.net/baigao66238598/article/details/102266390#p2886-usaco07nov%E7%89%9B%E7%BB%A7%E7%94%B5%E5%99%A8cow-relays">P2886 [USACO07NOV]牛继电器Cow Relays</a></li></ul></li></ul>
</div>
</div>
<h1 id="矩阵乘法与快速幂">矩阵乘法与快速幂</h1>
<hr>
<hr>
<h2 id="矩阵乘法">矩阵乘法</h2>
<p>定义矩阵<span class="math inline">\(A\)</span>,<span class="math inline">\(B\)</span>,其中<span class="math inline">\(A\)</span>的大小为<span class="math inline">\(a \times b\)</span>,<span class="math inline">\(B\)</span>的大小为<span class="math inline">\(b \times c\)</span>,对于矩阵<span class="math inline">\(C=AB\)</span>中的每一个元素<span class="math inline">\(C(i.j),~i\in [1, a],~j\in [1,c]\)</span>,存在以下:<br><span class="math display">\[ C(i,j) = \sum_{k=1}^bA(i,k) \times B(k,j) \]</span></p>
<h2 id="矩阵乘法的结合律">矩阵乘法的结合律</h2>
<p>矩阵乘法存在结合律,首先定义矩阵<span class="math inline">\(A_{a\times b},~B_{b\times c},~C_{c\times d}\)</span>,存在<span class="math inline">\((AB)C = A(BC)\)</span>。证明:</p>
<p>对于<span class="math inline">\(i\in [1, a],~j\in [1,d]\)</span>,有:<br><span class="math display">\[ \begin {align} ((AB)C)(i,j) &= \sum_{l=1}^c(\sum_{k=1}^b A(i,k)\times B(k,l))\times C(l,j) \\ &= \sum_{k=1}^b \sum_{l=1}^c A(i,k)\times B(k,l) \times C(l,k) \\ &= \sum_{k=1}^b A(i,k)\times (\sum_{l=1}^c B(k,l)\times C(l,k)) \\ &= (A(BC))(i,j) \end {align} \]</span><br> 由此,可以证明,矩阵的乘法具有结合律。</p>
<h2 id="floyd-算法">Floyd 算法</h2>
<p>Floyd算法的主要目的是在一张图上求任意两点之间的最短路,而最短路的核心思想其实可以由一个方程来表达:<br><span class="math display">\[ dis[i][j] = \min_{j \in [1,n]}\{dis[i][j],~ dis[i][k] + dis[k][j]\} \]</span><br> 其表示<span class="math inline">\(i \rightarrow j\)</span>的最短路。</p>
<p>但是它的初始化值得注意,其应该初始化为一个<strong>图论</strong>的<strong>单位矩阵</strong>,即主对角线的值为<span class="math inline">\(0\)</span>,其余值均为<span class="math inline">\(\infty\)</span>的一个“单位矩阵”,可以如下定义:<br><span class="math display">\[ dis[i][j] = \begin {cases} \infty ~~~~~~~&i≠j \\ ~0 &i=j \end {cases} \]</span><br> 为了实现上述思想,以下为代码实现:</p>
<pre class="blockcode"><code class="language-cpp"><code>for (int k = 1; k <= n; k ++)
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
if (m[i][k] + m[k][j] < m[i][j])
m[i][j] = m[i][k] + m[k][j];</code></code></pre>
<p><strong>注:</strong>Floyd算法的k的那层循环必须在最外面,否则有些值无法被更新。</p>
<h2 id="快速幂">快速幂</h2>
<p>如果我们要计算<span class="math inline">\(a^n\)</span>,则会将<span class="math inline">\(a\)</span>乘<span class="math inline">\(n\)</span>次,时间复杂度为<span class="math inline">\(O(n)\)</span>,而这太浪费时间了,为此,有了快速幂之中算法,快速幂可以将时间复杂度降为<span class="math inline">\(O(\log n)\)</span>,其本质就是将指数化为一个<span class="math inline">\(2\)</span>进制数来进行记忆化的乘法,现在假设有一个需求要求<span class="math inline">\(a^{13}\)</span>,我们可以试着将<span class="math inline">\(13\)</span>化为二进制:<span class="math inline">\(13=(1101)_2\)</span>,也就是说:<br><span class="math display">\[ a^{13} &# |
|