ACM模板库

ACM模板库


十进制矩阵快速幂

<pre><code class="language-cpp">#include &lt;iostream&gt; using namespace std; typedef long long ll; const ll mod = 1e9+7; struct Max{ ll a[5][5]; Max operator*(const Max &amp;m){ Max M; for(int i = 1; i &lt;= 2; i++){ for(int j = 1; j &lt;= 2; j++){ ll sum = 0; for(int k = 1; k &lt;= 2; k++){ sum = (sum + this-&gt;a[i][k] * m.a[k][j] % mod) % mod; } M.a[i][j] = sum; } } return M; } Max(){ this-&gt;a[1][1] = this-&gt;a[2][2] = 1; this-&gt;a[1][2] = this-&gt;a[2][1] = 0; } }; Max qpow(Max a, string s){ Max m; int n = s.size() - 1; while(n &gt;= 0){ int b = s[n] - '0'; for(int i = 1; i &lt;= b; i++) m = m * a; a = a * a; Max y = a * a; Max z = y * y; a = a * z; n--; } return m; }</code></pre>

页面列表

ITEM_HTML