版本比较
矩阵
正文
增加 105 行删除 105 行
73 行未改动747576777878798081828384858686878889903 行未改动9495969798989910010110220 行未改动1231241251261271271281291301315 行未改动13713813914014114114214314414510 行未改动1561571581591601601611621631641651651661671681697 行未改动1771781791801811811821831841851861861871881891908 行未改动1992002012022032032042052062072082082092102112128 行未改动2212222232242252252262272282296 行未改动2362372382392402402412422432442442452462472482492502512522532542542552562572584 行未改动2632642652662672672682692702713 行未改动27527627727827927928028128228315 行未改动2993003013023033033043053063073083093093103113123133143153163173183193193203213223237 行未改动33133233333433533533633733833934034034134234334416 行未改动3613623633643653663653663673683693703703713723733743753753763773783783793803813823833843843853863873883893903903913923933946 行未改动4014024034044054054064074084094104114114124134144154164164174184194206 行未改动42742842943043143143243343443543643743743843944044044144244344444544644744844945044844945045145245345345445545645745845845946046146229 行未改动4924934944954964964974984984995005015025035035045055065075085095105115115125135145155165175185195205215215225235245255265275285285295305315325335345355365375385385395405415425435445455465465475485485495505515525535535545555565575585595605615615625635645655665675685695705715715725735745755765775785795795805815825835845855865875885895895905915925935945955965975975985995996006016026036046056056066076086096106116126136136146156166172 行未改动6206216226236246246256266276286296306316316326336346356366376386396406416416426436446456466476486496496506516516526536546556566576586586596606616626636646656666666676686696703 行未改动67467567667767867867968068168268368468568668668768868969069169269369469569669669769869970061 行未改动7627637647657667677667677687697707717717727737747757767767777787797797807817827837847857857867877887897907917917927937947956 行未改动8028038048058068068078088098108118128128138148158168178178188198208216 行未改动82882983083183283283383483583683783883883984084184184284384484584684784884985085184985085185285385485485585685785885985986086186286386 行未改动9509519529539549559549559569579589599599609619629639649649659669679679689699709719729739739749759769779789799799809819829836 行未改动9909919929939949949959969979989991000100010011002100310041005100510061007100810096 行未改动101610171018101910201020102110221023102410251026102610271028102910291030103110321033103410351036103710381039103710381039104010411042104210431044104510461047104710481049105010519 行未改动106110621063106410651065106610671068106932 行未改动73 行未改动
代码实现如下:
```c++Matrix operator + (const Matrix& o) const{ // 加法 Matrix operator + (const Matrix& o) const{ // 加法 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]+o.arr[i][j])%m; return res;}``````c++Matrix operator - (const Matrix& o) const{ // 减法 Matrix operator - (const Matrix& o) const{ // 减法 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]-o.arr[i][j]+m)%m; return res;3 行未改动
## 数乘
$$k k
\begin{bmatrix}1 & 2 \\3 & 420 行未改动}```
```c++Matrix operator * (int k,const Matrix& mat){ // 数乘 Matrix operator * (int k,const Matrix& mat){ // 数乘 return mat*k;}```
5 行未改动
$$\begin{bmatrix}1 & 1 \\1 & 0 1 & 0\end{bmatrix}
\begin{bmatrix}1 & 1 & 1 & 1\\10 行未改动\begin{bmatrix}1 & 1 \\1 & 0 \\1 & 0 \\1 & 1 1 & 1\end{bmatrix}
\begin{bmatrix}1 & 1 \\1 & 0 1 & 0\end{bmatrix}$$
7 行未改动\begin{bmatrix}1 & 1 \\1 & 0 \\1 & 0 \\1 & 1 1 & 1\end{bmatrix}
\begin{bmatrix}1 & 1 \\1 & 0 1 & 0\end{bmatrix}$$
便最后会得到一个 4*2 的矩阵。8 行未改动\begin{bmatrix}1 & 1 \\1 & 0 \\1 & 0 \\1 & 1 1 & 1\end{bmatrix}
\begin{bmatrix}1 & 1 \\1 & 0 1 & 0\end{bmatrix}
=
8 行未改动
具体代码实现:
```c++Matrix operator * (const Matrix& o) const{ // 乘法 Matrix operator * (const Matrix& o) const{ // 乘法 Matrix res; rep(i,0,1) rep(j,0,1){ res.arr[i][j]=0; rep(k,0,1)6 行未改动## 转置
符号是:$$A^T \quad A^T \quad$$设:$$A = [a_{ij}]_{m \times n} A = [a_{ij}]_{m \times n}$$对于它的转置矩阵:$$(A^T)_{ij} = A_{ji}$$在此不过多阐述。
代码实现:```c++Matrix operator ~ () const { // 转置 Matrix operator ~ () const { // 转置 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=arr[j][i]; return res;4 行未改动
代码实现:
```c++int det() const{ //求行列式 int det() const{ //求行列式 int d=arr[0][0]*arr[1][1]%m; d=(d-arr[0][1]*arr[1][0]%m+m)%m; return d;}3 行未改动
代码实现:
```c++Matrix operator ! () const{ // 逆矩阵重载 Matrix operator ! () const{ // 逆矩阵重载 int d=det(); // if (d == 0) { // // 行列式为0,矩阵不可逆 // cerr << "Matrix is not invertible!" << endl;15 行未改动
代码实现:
```c++bool operator == (const Matrix& o) const{ // 相等 bool operator == (const Matrix& o) const{ // 相等 rep(i,0,1) rep(j,0,1) if(arr[i][j]!=o.arr[i][j]) return 0; return 1;}bool operator != (const Matrix& o) const{ // 不等 bool operator != (const Matrix& o) const{ // 不等 return !(*this==o);}```
## 矩阵快速幂
代码实现:
```c++Matrix ksm(int b) const{ // 快速幂 Matrix ksm(int b) const{ // 快速幂 Matrix ans=Matrix::Unit(); Matrix a=*this; for(;b;b>>=1){ if(b&1) ans=ans*a;7 行未改动
代码实现:
```c++istream& operator >> (istream& in, Matrix& mat){ // 输入 istream& operator >> (istream& in, Matrix& mat){ // 输入 rep(i,0,1) rep(j,0,1) in>>mat.arr[i][j]; return in;}ostream& operator << (ostream& out, const Matrix& mat){ // 输出 ostream& operator << (ostream& out, const Matrix& mat){ // 输出 rep(i,0,1){ rep(j,0,1) out<<mat.arr[i][j]<<" "; out<<"\n";16 行未改动 return res;}struct Matrix{ int arr[2][2]; Matrix(){memset(arr,0,sizeof arr);} // 初始化 Matrix(int a,int b,int c,int d){ // 快速构造 Matrix(){memset(arr,0,sizeof arr);} // 初始化 Matrix(int a,int b,int c,int d){ // 快速构造 arr[0][0]=a,arr[0][1]=b, arr[1][0]=c,arr[1][1]=d; } static Matrix Unit(){ // 单位矩阵 static Matrix Unit(){ // 单位矩阵 Matrix t; rep(i,0,1) t.arr[i][i]=1; return t; } static Matrix Zero(){ // 0 矩阵 static Matrix Zero(){ // 0 矩阵 return Matrix(); } Matrix operator + (const Matrix& o) const{ // 加法 Matrix operator + (const Matrix& o) const{ // 加法 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]+o.arr[i][j])%m; return res; } Matrix operator - (const Matrix& o) const{ // 减法 Matrix operator - (const Matrix& o) const{ // 减法 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]-o.arr[i][j]+m)%m; return res; } Matrix operator * (const Matrix& o) const{ // 乘法 Matrix operator * (const Matrix& o) const{ // 乘法 Matrix res; rep(i,0,1) rep(j,0,1){ res.arr[i][j]=0; rep(k,0,1)6 行未改动 rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]*k)%m; return res; } Matrix operator ~ () const { // 转置 Matrix operator ~ () const { // 转置 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=arr[j][i]; return res; } int det() const{ //求行列式 int det() const{ //求行列式 int d=arr[0][0]*arr[1][1]%m; d=(d-arr[0][1]*arr[1][0]%m+m)%m; return d; } Matrix operator ! () const{ // 逆矩阵重载 Matrix operator ! () const{ // 逆矩阵重载 int d=det(); if (d == 0) { // 行列式为0,矩阵不可逆 cerr << "Matrix is not invertible!" << endl;6 行未改动 res.arr[1][0]=(-arr[1][0]+m)%m*inv_d%m; res.arr[1][1]=arr[0][0]*inv_d%m; return res; } bool operator == (const Matrix& o) const{ // 相等 bool operator == (const Matrix& o) const{ // 相等 rep(i,0,1) rep(j,0,1) if(arr[i][j]!=o.arr[i][j]) return 0; return 1; } bool operator != (const Matrix& o) const{ // 不等 bool operator != (const Matrix& o) const{ // 不等 return !(*this==o); } Matrix ksm(int b) const{ // 快速幂 Matrix ksm(int b) const{ // 快速幂 Matrix ans=Matrix::Unit(); Matrix a=*this; for(;b;b>>=1){ if(b&1) ans=ans*a; a=a*a; } return ans; } }; Matrix operator * (int k,const Matrix& mat){ // 数乘 }};Matrix operator * (int k,const Matrix& mat){ // 数乘 return mat*k;}istream& operator >> (istream& in, Matrix& mat){ // 输入 istream& operator >> (istream& in, Matrix& mat){ // 输入 rep(i,0,1) rep(j,0,1) in>>mat.arr[i][j]; return in;}ostream& operator << (ostream& out, const Matrix& mat){ // 输出 ostream& operator << (ostream& out, const Matrix& mat){ // 输出 rep(i,0,1){ rep(j,0,1) out<<mat.arr[i][j]<<" "; out<<"\n";29 行未改动$$
$$\begin{flalign*}& &\begin{bmatrix}f_{i-1} & f_{i-2} f_{i-1} & f_{i-2}\end{bmatrix}
\begin{bmatrix}a & 1 \\b & 0 b & 0\end{bmatrix}
\\
& =
\begin{bmatrix}f_i & f_{i-1} f_i & f_{i-1}\end{bmatrix}
\end{flalign*}$$
令:$$M = \begin{bmatrix}a & 1 \\b & 0 b & 0\end{bmatrix}$$那么对于1.的递推矩阵:
$$\begin{bmatrix}f_2 & f_1 f_2 & f_1\end{bmatrix}\\cdot\M^{n-2}
=
\begin{bmatrix}f_n & f_{n-1} f_n & f_{n-1}\end{bmatrix}$$
#### 对于2:
$$\begin{flalign*}& &\begin{bmatrix}f_{i-1} & 3 f_{i-1} & 3\end{bmatrix}
\begin{bmatrix}2 & 0 \\1 & 1 1 & 1\end{bmatrix}
\\
& =
\begin{bmatrix}f_i & 3 f_i & 3\end{bmatrix}
\end{flalign*}$$
令:$$M = \begin{bmatrix}2 & 0 \\1 & 1 1 & 1\end{bmatrix}$$
那么对于2.的递推矩阵:
$$\begin{bmatrix}f_1 & 3 f_1 & 3\end{bmatrix}\\cdot\M^{n-1}
=
\begin{bmatrix}f_n & 3 f_n & 3\end{bmatrix}$$
#### 对于3:
$$\begin{flalign*}& &\begin{bmatrix}f_{i-1} & i-1 & 1 f_{i-1} & i-1 & 1\end{bmatrix}
\begin{bmatrix}2 & 0 & 0 \\1 & 1 & 0 \\1 & 1 & 1 1 & 1 & 1\end{bmatrix}
\\
& =
\begin{bmatrix}f_i & i & 1 f_i & i & 1\end{bmatrix}
\end{flalign*}$$2 行未改动$$M = \begin{bmatrix}2 & 0 & 0 \\1 & 1 & 0 \\1 & 1 & 1 1 & 1 & 1\end{bmatrix}$$那么对于3.的递推矩阵:
$$\begin{bmatrix}f_1 & 1 & 1 f_1 & 1 & 1\end{bmatrix}\\cdot\M^{n-1}
=
\begin{bmatrix}f_n & n & 1 f_n & n & 1\end{bmatrix}$$
#### 对于4:
$$\begin{flalign*}& &\begin{bmatrix}f_{i-1} & (i-1)^2 & i-1 & 1 f_{i-1} & (i-1)^2 & i-1 & 1\end{bmatrix}
\begin{bmatrix}3 & 0 & 0 & 0 \\1 & 1 & 0 & 0 \\3 & 2 & 1 & 0 \\3 & 1 & 1 & 1 3 & 1 & 1 & 1\end{bmatrix}
\\
& =
\begin{bmatrix}f_i & i^2 & i & 1 f_i & i^2 & i & 1\end{bmatrix}
\end{flalign*}$$3 行未改动M = \begin{bmatrix}3 & 0 & 0 & 0 \\1 & 1 & 0 & 0 \\3 & 2 & 1 & 0 \\3 & 1 & 1 & 1 3 & 1 & 1 & 1\end{bmatrix}$$
那么对于4.的递推矩阵:
$$\begin{bmatrix}f_1 & 1 & 1 & 1 f_1 & 1 & 1 & 1\end{bmatrix}\\cdot\M^{n-1}
=
\begin{bmatrix}f_n & n^2 & n & 1 f_n & n^2 & n & 1\end{bmatrix}$$
### 解释61 行未改动 return res;}struct Matrix{ int arr[105][105]; Matrix(){memset(arr,0,sizeof arr);} // 初始化 // Matrix(int a,int b,int c,int d){ // 快速构造 Matrix(){memset(arr,0,sizeof arr);} // 初始化// Matrix(int a,int b,int c,int d){ // 快速构造// arr[0][0]=a,arr[0][1]=b,// arr[1][0]=c,arr[1][1]=d;// } static Matrix Unit(){ // 单位矩阵 static Matrix Unit(){ // 单位矩阵 Matrix t; rep(i,0,n-1) t.arr[i][i]=1; return t; } static Matrix Zero(){ // 0 矩阵 static Matrix Zero(){ // 0 矩阵 return Matrix(); } Matrix operator + (const Matrix& o) const{ // 加法 Matrix operator + (const Matrix& o) const{ // 加法 Matrix res; rep(i,0,n-1) rep(j,0,n-1) res.arr[i][j]=(arr[i][j]+o.arr[i][j])%P; return res; } Matrix operator - (const Matrix& o) const{ // 减法 Matrix operator - (const Matrix& o) const{ // 减法 Matrix res; rep(i,0,n-1) rep(j,0,n-1) res.arr[i][j]=(arr[i][j]-o.arr[i][j]+P)%P; return res; } Matrix operator * (const Matrix& o) const{ // 乘法 Matrix operator * (const Matrix& o) const{ // 乘法 Matrix res; rep(i,0,n-1) rep(j,0,n-1){ res.arr[i][j]=0; rep(k,0,n-1)6 行未改动 rep(i,0,n-1) rep(j,0,n-1) res.arr[i][j]=(arr[i][j]*k)%P; return res; } Matrix operator ~ () const { // 转置 Matrix operator ~ () const { // 转置 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=arr[j][i]; return res; } int det() const{ //求行列式 int det() const{ //求行列式 int d=arr[0][0]*arr[1][1]%P; d=(d-arr[0][1]*arr[1][0]%P+P)%P; return d; } Matrix operator ! () const{ // 逆矩阵重载 Matrix operator ! () const{ // 逆矩阵重载 int d=det(); if (d == 0) { // 行列式为0,矩阵不可逆 cerr << "Matrix is not invertible!" << endl;6 行未改动 res.arr[1][0]=(-arr[1][0]+P)%P*inv_d%P; res.arr[1][1]=arr[0][0]*inv_d%P; return res; } bool operator == (const Matrix& o) const{ // 相等 bool operator == (const Matrix& o) const{ // 相等 rep(i,0,n-1) rep(j,0,n-1) if(arr[i][j]!=o.arr[i][j]) return 0; return 1; } bool operator != (const Matrix& o) const{ // 不等 bool operator != (const Matrix& o) const{ // 不等 return !(*this==o); } Matrix ksm(int b) const{ // 快速幂 Matrix ksm(int b) const{ // 快速幂 Matrix ans=Matrix::Unit(); Matrix a=*this; for(;b;b>>=1){ if(b&1) ans=ans*a; a=a*a; } return ans; } }; Matrix operator * (int k,const Matrix& mat){ // 数乘 }};Matrix operator * (int k,const Matrix& mat){ // 数乘 return mat*k;}istream& operator >> (istream& in, Matrix& mat){ // 输入 istream& operator >> (istream& in, Matrix& mat){ // 输入 rep(i,0,n-1) rep(j,0,n-1) in>>mat.arr[i][j]; return in;}ostream& operator << (ostream& out, const Matrix& mat){ // 输出 ostream& operator << (ostream& out, const Matrix& mat){ // 输出 rep(i,0,n-1){ rep(j,0,n-1) out<<mat.arr[i][j]<<" "; out<<"\n";86 行未改动 return res;}struct Matrix{ int arr[2][2]; Matrix(){memset(arr,0,sizeof arr);} // 初始化 Matrix(int a,int b,int c,int d){ // 快速构造 Matrix(){memset(arr,0,sizeof arr);} // 初始化 Matrix(int a,int b,int c,int d){ // 快速构造 arr[0][0]=a,arr[0][1]=b, arr[1][0]=c,arr[1][1]=d; } static Matrix Unit(){ // 单位矩阵 static Matrix Unit(){ // 单位矩阵 Matrix t; rep(i,0,1) t.arr[i][i]=1; return t; } static Matrix Zero(){ // 0 矩阵 static Matrix Zero(){ // 0 矩阵 return Matrix(); } Matrix operator + (const Matrix& o) const{ // 加法 Matrix operator + (const Matrix& o) const{ // 加法 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]+o.arr[i][j])%m; return res; } Matrix operator - (const Matrix& o) const{ // 减法 Matrix operator - (const Matrix& o) const{ // 减法 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]-o.arr[i][j]+m)%m; return res; } Matrix operator * (const Matrix& o) const{ // 乘法 Matrix operator * (const Matrix& o) const{ // 乘法 Matrix res; rep(i,0,1) rep(j,0,1){ res.arr[i][j]=0; rep(k,0,1)6 行未改动 rep(i,0,1) rep(j,0,1) res.arr[i][j]=(arr[i][j]*k)%m; return res; } Matrix operator ~ () const { // 转置 Matrix operator ~ () const { // 转置 Matrix res; rep(i,0,1) rep(j,0,1) res.arr[i][j]=arr[j][i]; return res; } int det() const{ //求行列式 int det() const{ //求行列式 int d=arr[0][0]*arr[1][1]%m; d=(d-arr[0][1]*arr[1][0]%m+m)%m; return d; } Matrix operator ! () const{ // 逆矩阵重载 Matrix operator ! () const{ // 逆矩阵重载 int d=det();// if (d == 0) {// // 行列式为0,矩阵不可逆// cerr << "Matrix is not invertible!" << endl;6 行未改动 res.arr[1][0]=(-arr[1][0]+m)%m*inv_d%m; res.arr[1][1]=arr[0][0]*inv_d%m; return res; } bool operator == (const Matrix& o) const{ // 相等 bool operator == (const Matrix& o) const{ // 相等 rep(i,0,1) rep(j,0,1) if(arr[i][j]!=o.arr[i][j]) return 0; return 1; } bool operator != (const Matrix& o) const{ // 不等 bool operator != (const Matrix& o) const{ // 不等 return !(*this==o); } Matrix ksm(int b) const{ // 快速幂 Matrix ksm(int b) const{ // 快速幂 Matrix ans=Matrix::Unit(); Matrix a=*this; for(;b;b>>=1){ if(b&1) ans=ans*a; a=a*a; } return ans; } }; //Matrix operator * (int k,const Matrix& mat){ // 数乘 }};//Matrix operator * (int k,const Matrix& mat){ // 数乘// return mat*k;//}istream& operator >> (istream& in, Matrix& mat){ // 输入 istream& operator >> (istream& in, Matrix& mat){ // 输入 rep(i,0,1) rep(j,0,1) in>>mat.arr[i][j]; return in;}ostream& operator << (ostream& out, const Matrix& mat){ // 输出 ostream& operator << (ostream& out, const Matrix& mat){ // 输出 rep(i,0,1){ rep(j,0,1) out<<mat.arr[i][j]<<" "; out<<"\n";9 行未改动 return; }// M.arr[0][0]=p,M.arr[0][1]=1,// M.arr[1][0]=q,M.arr[1][1]=0; Matrix M(p,1,q,0); Matrix M(p,1,q,0); M=M.ksm(n-2); Matrix ans=a*M; echo(ans.arr[0][0]%m,'\n'); return;32 行未改动