Martix operator *(Martix b){
Martix 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] = (c.a[i][j] % mod + (a[i][k] % mod * b.a[k][j] % mod)) % mod;
}
}
}
return c;
}
Part-4 例题
Part-4-1 B2105 矩阵乘法
只用乘一次。
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
struct Martix{
int n, m;
int a[105][105];
Martix():n(0),m(0){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix(int x, int y):n(x),m(y){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix operator *(Martix b){
Martix 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] = (c.a[i][j] + (a[i][k] * b.a[k][j]));
}
}
}
return c;
}
};
void work(){
//please write your code on here
int n = read(), m = read(), k = read();
Martix A(n, m);
Martix B(m, k);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
A.a[i][j] = read();
}
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= k; j++){
B.a[i][j] = read();
}
}
Martix C = A * B;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
write(C.a[i][j]);
putchar(' ');
}
putchar('\n');
}
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}
Part-4-2 P3390 【模板】矩阵快速幂
模版。
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
AK mod = 1e9 + 7;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
struct Martix{
int n, m;
int a[105][105];
Martix():n(0),m(0){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix(int k):n(k),m(k){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
for(int i = 1; i <= k; i++){
a[i][i] = 1;
}
}
Martix(int x, int y):n(x),m(y){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix operator *(Martix b){
Martix 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] = (c.a[i][j] % mod + (a[i][k] % mod * b.a[k][j] % mod)) % mod;
}
}
}
return c;
}
};
int n, k;
Martix power(Martix A, int b){
Martix ans(n);
while(b){
if(b & 1){
ans = ans * A;
// ans %= mod;
}
A = A * A;
b >>= 1;
}
return ans;
}
void work(){
//please write your code on here
n = read(), k = read();
Martix A(n, n);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
A.a[i][j] = read();
}
}
Martix C = power(A, k);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << (C.a[i][j] % (1000000007));
putchar(' ');
}
putchar('\n');
}
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}
Part-4-3 P1349 广义斐波那契数列
推出矩阵
\begin{bmatrix}
0 & q \\ p & 1
\end{bmatrix}
为单位矩阵
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int mod = 0;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int p, q;
struct Martix{
int n, m;
int a[105][105];
Martix(int x, int y):n(2),m(2){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix operator *(Martix b){
Martix 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] = (c.a[i][j] % mod + (a[i][k] % mod * b.a[k][j] % mod)) % mod;
}
}
}
return c;
}
};
int n, m, a1, a2;
Martix power(Martix A, int b){
Martix ans(2, 2);
for(int i = 1; i <= 2; i++){
ans.a[i][i] = 1;
}
while(b){
if(b & 1){
ans = ans * A;
// ans %= mod;
}
A = A * A;
b >>= 1;
}
return ans;
}
void work(){
//please write your code on here
cin >> p >> q >> a1 >> a2 >> n >> m;
Martix tmp(2, 2);
tmp.a[1][1] = 0;
tmp.a[1][2] = q;
tmp.a[2][1] = 1;
tmp.a[2][2] = p;
mod = m;
tmp = power(tmp, n - 1);
cout << (a1 * tmp.a[1][1] + a2 * tmp.a[2][1]) % mod;
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n , mod = 1e9 + 7;
struct Martix{
int n, m;
int a[105][105];
Martix():n(0),m(0){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix(int k):n(k),m(k){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
for(int i = 1; i <= k; i++){
a[i][i] = 1;
}
}
Martix(int x, int y):n(x),m(y){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix operator *(Martix b){
Martix 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] = (c.a[i][j] % mod + (a[i][k] % mod * b.a[k][j] % mod)) % mod;
}
}
}
return c;
}
};
Martix power(Martix A, int b){
Martix ans(3);
while(b){
if(b & 1){
ans = ans * A;
// ans %= mod;
}
A = A * A;
b >>= 1;
}
return ans;
}
void work() {
//please write your code on here
cin >> n ;
Martix a(1 , 3) ;
a.a[1][1] = 1 ;
a.a[1][2] = 2 ;
a.a[1][3] = 4 ;
if(n <= 3) {
cout << a.a[1][n] ;
return ;
}
Martix b(3 , 3) ;
b.a[2][1] = 1 ;
b.a[3][2] = 1 ;
b.a[1][3] = b.a[2][3] = b.a[3][3] = 1 ;
b = power(b , n - 1) ;
a = a * b ;
cout << a.a[1][1] ;
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}
Part-4-5 P2044 [NOI2012] 随机数生成器
推出数列
\begin{bmatrix}
a & 0 \\ 1 & 1
\end{bmatrix}
而基础数列是
\begin{bmatrix}
a_0 & c
\end{bmatrix}
注意:有可能爆 long long。
不会int128 T_T
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int mod , aa , c , x0 , n , g ;
struct Martix{
int n, m;
int a[105][105];
Martix():n(0),m(0){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix(int k):n(k),m(k){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
for(int i = 1; i <= k; i++){
a[i][i] = 1;
}
}
Martix(int x, int y):n(x),m(y){
for(int i = 1; i <= m; i++){
fill(a[i] + 1, a[i] + 1 + n, 0);
}
}
Martix operator *(Martix b){
Martix 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] = (c.a[i][j] % mod + (a[i][k] % mod * b.a[k][j] % mod)) % mod;
}
}
}
return c;
}
};
Martix power(Martix A, int b){
Martix ans(3);
while(b){
if(b & 1){
ans = ans * A;
// ans %= mod;
}
A = A * A;
b >>= 1;
}
return ans;
}
void work() {
//please write your code on here
mod = read() , aa = read() , c = read() , x0 = read() , n = read() , g = read() ;
Martix a(1 , 2) ;
a.a[1][1] = x0 ;
a.a[1][2] = c ;
Martix b(2 , 2) ;
b.a[1][1] = aa ;
b.a[2][1] = 1 ;
b.a[1][2] = 0 ;
b.a[2][2] = 1 ;
b = power(b , n) ;
a = a * b ;
write(a.a[1][1] % g) ;
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}
Part-4-6 P3758 [TJOI2017] 可乐
floyd 加速板子。
发现现在的邻接表乘上自己,就是再走一次的邻接表,因此,这题加上一条自己和自己的边,就可以表示停留,设 i = 0 表示已经爆炸,连接从 1 到 n 的所有点到 0 的边,就可以表示爆炸。最后直接统计 1 的个数。
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n , m , t , ans , mod = 2017;
struct Martix{
int n, m;
int a[105][105];
Martix(){
memset(a , 0 , sizeof a) ;
}
Martix operator * (const Martix b){
Martix c ;
for(int i = 0; i <= 30; i++){
for(int j = 0; j <= 30; j++){
for(int k = 0; k <= 30; k++){
c.a[i][j] += a[i][k] * b.a[k][j];
c.a[i][j] %= mod;
}
}
}
return c ;
}
};
Martix power(Martix a, int b){
Martix mul ;
for( int i = 0 ; i <= 30 ; i++ ) mul.a[i][i] = 1 ;
while(b){
if(b & 1) mul = mul * a ;
a = a * a ;
b >>= 1 ;
}
return mul ;
}
void work() {
//please write your code on here
cin >> n >> m ;
Martix a ;
for(int i = 1; i <= m; i++){
int u , v ;
cin >> u >> v ;
a.a[u][v] = a.a[v][u] = 1 ;
}
for(int i = 0; i <= n; i++) a.a[i][i] = 1 ;
for(int i = 1; i <= n; i++) a.a[i][0] = 1 ;
cin >> t ;
a = power(a , t) ;
for(int i = 0 ; i <= n; i++){
ans += a.a[1][i];
ans %= mod;
}
cout << ans;
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}
-----本文70\%内容为作者自己所写,25\%借助笔记,5\%借助deepseek, oiwiki and somebody's article