【1】2020.05.21-23:49ios
1.完善矩陣快速冪學習
因爲普通快速冪太過於簡單,這裏就先不寫了,後期再完善吧QAQ
在學習矩陣快速冪以前,咱們先來了解一下矩陣這個東西spa
矩陣的定義:在數學中,矩陣是一個按照長方陣列排列的複數或實數集合code
好了相信你已經精通瞭解了矩陣
接下來讓咱們接觸一下它的運算ci
那麼首先咱們要明白:不是任意兩個矩陣均可以相乘
若是兩個矩陣能夠相乘,那麼其中一個矩陣的行數等於另外一個矩陣的列數數學
舉幾個例子:string
設A爲i×k矩陣,B爲k×j矩陣,那麼其乘積爲一個i×j矩陣it
以後記公式便可io
\(C_{i,j}=\sum\limits_{k=1}^{k}A_{i,k}*B_{k,j}\)class
舉一個很簡單的例子:設A爲2×4矩陣,B爲4×2矩陣
\(A=\begin{bmatrix}0&1&2&3\\4&5&6&7\end{bmatrix}\)
\(B=\begin{bmatrix}10&11\\12&13\\14&15\\16&17\end{bmatrix}\)
\(A*B=\)
\(\begin{bmatrix}0*10+1*12+2*14+3*16&0*11+1*13+2*15+3*17\\4*10+5*12+6*14+7*16&4*11+5*13+6*15+7*17\end{bmatrix}\)
\(=\begin{bmatrix}88&94\\296&318\end{bmatrix}\)
那麼接下來就很簡單了,重載一下乘號,原樣寫代碼就能夠
(普通快速冪怎麼寫就怎麼寫)
#include<iostream> #include<cstring> using namespace std; #define NUM 105 #define MOD 1000000007 #define ll long long ll n,f; struct matrix{ ll a[NUM][NUM]; matrix() {memset(a,0,sizeof a);} }m,ans; matrix operator * (matrix const &A,matrix const &B){ matrix C;int i,o,p; for(i=1;i<=n;i++) for(o=1;o<=n;o++) for(p=1;p<=n;p++) C.a[i][o]=(C.a[i][o]+A.a[i][p]*B.a[p][o]%MOD)%MOD; return C; } int main(){ cin>>n>>f; int i,o; for(i=1;i<=n;i++){ ans.a[i][i]=1; for(o=1;o<=n;o++) cin>>m.a[i][o]; } while(f){ if(f&1) ans=ans*m; f>>=1;m=m*m; } for(i=1;i<=n;i++){ for(o=1;o<=n;o++) cout<<ans.a[i][o]<<" "; cout<<"\n"; } }