跳到正文

个人记录

悬线法与矩阵

_yang_yi_bo_约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 13 分钟

版本

  1. v1当前

P1387 最大正方形

这里介绍此题 O(n2)O(n^2) 解法。

令 dpi.jdp_{i.j} 表示以 (i,j)(i,j) 结尾的不包含 00 的最大正方形的边长。

则 dpi.jdp_{i.j} 的答案是以 (i−1,j−1)(i-1,j-1) 结尾的不包含 00 的最大正方形的边长与从 (i,j−1)(i,j-1) 往左走的最长全 00 序列长度与从 (i−1,j)(i-1,j) 往上走的最长全 00 序列长度。

其中 (i,j−1)(i,j-1) 往左走的最长全 00 序列长度与从 (i−1,j)(i-1,j) 往上走的最长全 00 序列长度可以转化为 dpi,j−1dp_{i,j-1} 与 dpi−1,jdp_{i-1,j}。

没了。

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int dp[105][105];
bool vis[105][105];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>vis[i][j];
		}
	}for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(vis[i][j])dp[i][j]=min(min(dp[i][j-1],dp[i-1][j]),dp[i-1][j-1])+1;
			ans=max(ans,dp[i][j]);
		}
	}cout<<ans;
	return 0;
}

P1736 创意吃鱼法

这道题和上一道题差不多,只不过其中 (i,j−1)(i,j-1) 往左走的最长全 00 序列长度与从 (i−1,j)(i-1,j) 往上走的最长全 00 序列长度不可以转化为 dpi,j−1与dpi−1,jdp_{i,j-1} 与 dp_{i-1,j}。

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
bool vis[2505][2505];
int dp1[2505][2505];
int dp2[2505][2505];
int h[2505][2505];
int s[2505][2505];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>vis[i][j];
			if(!vis[i][j]){
				h[i][j]=h[i][j-1]+1;
				s[i][j]=s[i-1][j]+1;
			}else{
				dp1[i][j]=min(dp1[i-1][j-1],min(h[i][j-1],s[i-1][j]))+1;
				ans=max(ans,dp1[i][j]);
			}//cout<<dp1[i][j]<<" ";
		}//cout<<"\n";
	}memset(h,0,sizeof h);
	for(int i=1;i<=n;i++){
		for(int j=m;j>=1;j--){
			if(!vis[i][j]){
				h[i][j]=h[i][j+1]+1;
			}else{
				dp2[i][j]=min(dp2[i-1][j+1],min(h[i][j+1],s[i-1][j]))+1;
				ans=max(ans,dp2[i][j]);
			}
		}
	}cout<<ans;
	return 0;
}

B2105 矩阵乘法

矩阵,可以理解成一个二维数组:

A=[1355]A=\begin{bmatrix} 1 & 3\\ 5 & 5 \end{bmatrix}

其有 22 种操作:

第一种

把矩阵 A×A\times 一个常数 kk。

假设 A=[abcd]A=\begin{bmatrix} a & b\\ c & d \end{bmatrix},则 A×k=[a×kb×kc×kd×k]A \times k =\begin{bmatrix} a\times k & b\times k\\ c\times k & d\times k \end{bmatrix}。

第二种

把矩阵 A×A\times 一个矩阵 BB。

假设 A=[abcdef]A=\begin{bmatrix} a & b\\ c & d\\ e & f \end{bmatrix},B=[xyzw]B=\begin{bmatrix} x & y\\ z & w \end{bmatrix} 则 A×B=[a×x+b×za×y+b×wc×x+d×zc×y+d×we×x+f×ze×y+f×w]A \times B =\begin{bmatrix} a \times x + b \times z & a \times y + b \times w\\ c \times x + d \times z & c \times y + d \times w\\ e \times x + f \times z & e \times y + f \times w \end{bmatrix}。

特别的是,AA 的列数必须等于 BB 的行数

我们可以把这些东西定义在一个结构体里。

比如定义一个 22 行 33 列的矩阵 AA。

C++
struct ccf{
	int n,m;
	int a[105][105];
	ccf():n(0),m(0){
		memset(a,0,sizeof a);
	}
	ccf(int x,int y):n(x),m(y){
		memset(a,0,sizeof a);
	}
	ccf operator *(ccf b){
		ccf c(n,b.m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=b.m;j++){
				for(int k=1;k<=m;k++){
					c.a[i][j]+=a[i][k]*b.a[k][j];
				}
			}
		}return c;
	}
  ccf operator ~(int k){
		ccf c(n,m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
        c.a[i][j]+=a[i][j]*k;
			}
		}return c;
	}
}a(2,3);

下题的代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
struct ccf{
	int n,m;
	int a[105][105];
	ccf():n(0),m(0){
		memset(a,0,sizeof a);
	}
	ccf(int x,int y):n(x),m(y){
		memset(a,0,sizeof a);
	}
	ccf operator *(ccf b){
		ccf c(n,b.m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=b.m;j++){
				for(int k=1;k<=m;k++){
					c.a[i][j]+=a[i][k]*b.a[k][j];
				}
			}
		}return c;
	}
};
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>k;
	ccf a(n,m),b(m,k);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a.a[i][j];
		}
	}for(int i=1;i<=m;i++){
		for(int j=1;j<=k;j++){
			cin>>b.a[i][j];
		}
	}ccf c=a*b;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=k;j++){
			cout<<c.a[i][j]<<" ";
		}cout<<"\n";
	}
	return 0;
}

P3390 【模板】矩阵快速幂

容易发现,如果矩阵 BB 的主对角线全是 11,其余全是 00,则对于矩阵 AA,A×B=AA \times B=A。

这个矩阵就叫单位矩阵,相当于数值 11。

在矩阵上进行快速幂和普通快速幂一模一样,都是运用了如下原理:

ak={(ak2)2(k mod 2=0)(a⌊k2⌋)2×k(k mod 2≠0)a^k=\left\{\begin{matrix} {(a^{\frac{k}{2}}})^2(k \bmod 2 = 0) \\ {(a^{\lfloor\frac{k}{2}\rfloor}})^2 \times k(k\bmod 2 \ne 0) \end{matrix}\right.
C++
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
struct ccf{
	int n,m;
	int a[105][105];
	ccf():n(0),m(0){
		memset(a,0,sizeof a);
	}
	ccf(int x,int y):n(x),m(y){
		memset(a,0,sizeof a);
	}
	ccf operator *(ccf b){
		ccf c(n,b.m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=b.m;j++){
				for(int k=1;k<=m;k++){
					c.a[i][j]+=a[i][k]*b.a[k][j];
				}
			}
		}return c;
	}
};
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>k;
	ccf a(n,m),b(m,k);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a.a[i][j];
		}
	}for(int i=1;i<=m;i++){
		for(int j=1;j<=k;j++){
			cin>>b.a[i][j];
		}
	}ccf c=a*b;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=k;j++){
			cout<<c.a[i][j]<<" ";
		}cout<<"\n";
	}
	return 0;
}

P1349 广义斐波那契数列

这里是矩阵快速幂的应用。

如何快速求出广义斐波那契数列的第 nn 项。

定义矩阵 A=[F1F2],B=[0q1p]A=\begin{bmatrix} F_1&F_2 \end{bmatrix},B=\begin{bmatrix} 0&q \\ 1&p \end{bmatrix},其中 FiF_i 表示斐波那契数列的第 ii 项。

容易得出 A×B=[F2F3]A\times B=\begin{bmatrix} F_2&F_3 \end{bmatrix}。

所以 A×Bn−2=[Fn−1Fn]A\times B^{n-2}=\begin{bmatrix} F_{n-1}&F_{n} \end{bmatrix}。

即可快速求解。

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int p, q, a1, a2, n, mod;
struct ccf {
  int n, m;
  int a[105][105];
  ccf() : n(0), m(0) {
    memset(a, 0, sizeof a);
  }
  ccf(int x, int y) : n(x), m(y) {
    memset(a, 0, sizeof a);
  }
  ccf(int k) : n(k), m(k) {
    memset(a, 0, sizeof a);
    for (int i = 1; i <= k; i++) a[i][i] = 1;
  }
  ccf operator*(ccf b) {
    ccf c(n, b.m);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= b.m; j++) {
        for (int k = 1; k <= m; k++) {
          c.a[i][j] += (a[i][k] * b.a[k][j]) % mod;
          c.a[i][j] %= mod;
        }
      }
    }
    return c;
  }
};
ccf qpow(ccf a, int b) {
  ccf ans(2);
  while (b) {
    if (b & 1) ans = ans * a;
    a = a * a;
    b >>= 1;
  }
  return ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> p >> q >> a1 >> a2 >> n >> mod;
  if (n == 1) {
    cout << a1;
    return 0;
  }
  if (n == 2) {
    cout << a2;
    return 0;
  }
  ccf a(2, 2);
  a.a[1][1] = 0;
  a.a[2][1] = 1;
  a.a[1][2] = q;
  a.a[2][2] = p;
  ccf b(1, 2);
  b.a[1][1] = a1, b.a[1][2] = a2;
  ccf ans(1, 2);
  ans = b * qpow(a, n - 2);
  cout << ans.a[1][2] << " ";
  return 0;
}
原文