跳到正文

个人记录

矩阵快速幂

Hhy140516约 34 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

基础

矩阵(数学)——二位数组(计算机)

矩阵的三种功能:

  1. 解方程(省选)
  2. 加速线性 DP(S组)
  3. 加速 floyd(S组)

性质:

1.常数乘法:假设

A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}

则

k∗A=[1∗k2∗k3∗k4∗k]k*A = \begin{bmatrix} 1*k & 2*k \\ 3*k & 4*k \end{bmatrix}

2.矩阵乘法

A=[abcd]B=[xyzwub]A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\\ B = \begin{bmatrix} x & y & z \\ w & u & b \end{bmatrix}

则

A×B=[ax+bway+buaz+bvcx+dwcy+ducz+dv]A \times B = \begin{bmatrix} ax + bw & ay + bu & az + bv \\ cx + dw & cy + du & cz + dv \end{bmatrix}

AA 矩阵的第一行乘 BB 矩阵的第 jj 列等于 CC 矩阵的第 ii 行第 jj 列。

假如 AA 是 nn 行 mm 列,BB 是 mm 行 kk 列,则 CC 是 nn 行 kk 列。

[1234]×[1001]=[1234][10⋯001⋯0⋮⋮⋱⋮00⋯1]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \times \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \\ \begin{bmatrix} 1 & 0 & \cdots & 0 \\ 0 & 1 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & 1 \end{bmatrix}

就是单位矩阵。

普通的斐波那契数列 Fi=Fi−1+Fi−2F_i = F_{i - 1} + F_{i - 2},可以通过矩阵乘法求出FnF_n。

可以发现,每次 Fi=Fi−1+Fi−2F_i = F_{i - 1} + F_{i - 2},Fi−1=Fi−1F_{i - 1} = F_{i - 1},通过矩阵乘法能做到这样的操作,每次乘的矩阵是

[0111]\begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix}

而要乘 n−2n - 2 次,自然想到优化是快速幂,就是

[F1F2]×[0111]n−2=[Fn−1Fn]\begin{bmatrix} F_1 & F_2 \end{bmatrix} \times { \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix} } ^ {n - 2} = \begin{bmatrix} F_{n - 1} & F_n \end{bmatrix}

结合律

A×B×C=(A×B)×C=A×(B×C)A \times B \times C = (A \times B) \times C = A \times (B \times C)

而

A×B≠B×AA \times B \ne B \times A

例题

B2105 矩阵乘法

只用乘一次。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#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";\
}
using namespace std ;
const int kMaxN = 105 , MOD = 998244353 , INF = 1e9 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
struct node
{
  int n , m , a[kMaxN][kMaxN] ;
  node(int x , int y)
  {
  	n = x ;
  	m = y ;
  	memset(a , 0 , sizeof a) ;
	}
	node operator * (const node b)
	{
		node 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 ;
	}
};
inline ll ksm(ll a , ll b)
{
	ll mul = 1 ;
	while(b)
	{
		if(b & 1) mul *= a , mul %= MOD ;
		a *= a ;
		a %= MOD ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
int n , m , k ;
void init()
{

}
void work()
{
  init() ;
  cin >> n >> m >> k ;
  node 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] ;
		}
	}
	node c(n , k) ;
	c = a * b ;
	for( int i = 1 ; i <= n ; i++ )
  {
  	for( int j = 1 ; j <= k ; j++ )
  	{
  		cout << c.a[i][j] << " " ;
		}
		cout << "\n" ;
	}
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}

P3390 【模板】矩阵快速幂

模版。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#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";\
}
using namespace std ;
const int kMaxN = 105 , MOD = 1e9 + 7 , INF = 1e9 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
int n , k ;
struct node
{
  int n , m , a[kMaxN][kMaxN] ;
  node(int x , int y)
  {
  	n = x ;
  	m = y ;
  	memset(a , 0 , sizeof a) ;
	}
	node operator * (const node b)
	{
		node 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] ;
					c.a[i][j] %= MOD ;
				}
			}
		}
		return c ;
	}
};
node ksm(node a , ll b)
{
	node mul(n , n) ;
	for( int i = 1 ; i <= n ; i++ ) mul.a[i][i] = 1 ;
	while(b)
	{
		if(b & 1) mul = mul * a ;
		a = a * a ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void init()
{

}
void work()
{
  init() ;
  cin >> n >> k ;
  node a(n , n) ;
  for( int i = 1 ; i <= n ; i++ )
  {
  	for( int j = 1 ; j <= n ; j++ )
  	{
  		cin >> a.a[i][j] ;
		}
	}
	node c(n , n) ;
	c = ksm(a , k) ;
	for( int i = 1 ; i <= n ; i++ )
  {
  	for( int j = 1 ; j <= n ; j++ )
  	{
  		cout << c.a[i][j] << " " ;
		}
		cout << "\n" ;
	}
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}

P1349 广义斐波那契数列

推出矩阵

[0q1p]\begin{bmatrix} 0 & q \\ 1 & p \end{bmatrix}

注意:n≤2n \le 2 时特判。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#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";\
}
using namespace std ;
const int kMaxN = 105 , MOD = 1e9 + 7 , INF = 1e9 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
int p , q , a1 , a2 , n , mod ;
struct node
{
  int n , m , a[kMaxN][kMaxN] ;
  node(int x , int y)
  {
  	n = x ;
  	m = y ;
  	memset(a , 0 , sizeof a) ;
	}
	node operator * (const node b)
	{
		node 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] ;
					c.a[i][j] %= mod ;
				}
			}
		}
		return c ;
	}
};
node ksm(node a , ll b)
{
	node mul(2 , 2) ;
	for( int i = 1 ; i <= 2 ; i++ ) mul.a[i][i] = 1 ;
	while(b)
	{
		if(b & 1) mul = mul * a ;
		a = a * a ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void init()
{

}
void work()
{
  init() ;
  cin >> p >> q >> a1 >> a2 >> n >> mod ;
  if(n == 1) {cout << a1 ; return ;}
  if(n == 2) {cout << a2 ; return ;}
  node a(2 , 2) , b(1 , 2) ;
  a.a[2][1] = 1 ;
  a.a[1][2] = q ;
	a.a[2][2] = p ;
	a = ksm(a , n - 2) ;
	b.a[1][1] = a1 ;
	b.a[1][2] = a2 ;
	b = b * a ;
	cout << b.a[1][2] ;
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}

U546616 骨牌

发现 Fi=Fi−1+Fi−2+Fi−3F_i = F_{i - 1} + F_{i - 2} + F_{i - 3},推出数列

[001101011]\begin{bmatrix} 0 & 0 & 1 \\ 1 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix}

注意:n≤3n \le 3 时特判。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#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";\
}
using namespace std ;
const int kMaxN = 105 , MOD = 1e9 + 7 , INF = 1e9 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
int n ;
struct node
{
  int n , m , a[kMaxN][kMaxN] ;
  node(int x , int y)
  {
  	n = x ;
  	m = y ;
  	memset(a , 0 , sizeof a) ;
	}
	node operator * (const node b)
	{
		node 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] ;
					c.a[i][j] %= MOD ;
				}
			}
		}
		return c ;
	}
};
node ksm(node a , ll b)
{
	node mul(3 , 3) ;
	for( int i = 1 ; i <= 3 ; i++ ) mul.a[i][i] = 1 ;
	while(b)
	{
		if(b & 1) mul = mul * a ;
		a = a * a ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void init()
{

}
void work()
{
  init() ;
  cin >> n ;
  node 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 ;
	}
	node 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 = ksm(b , n - 1) ;
	a = a * b ;
	cout << a.a[1][1] ;
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}

P2044 [NOI2012] 随机数生成器

推出数列

[a011]\begin{bmatrix} a & 0 \\ 1 & 1 \end{bmatrix}

而基础数列是

[a0c]\begin{bmatrix} a_0 & c \end{bmatrix}

注意:有可能爆 long long。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int __int128
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#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";\
}
using namespace std ;
const int kMaxN = 105 , MOD = 1e9 + 7 , INF = 1e9 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
int mod , aa , c , x0 , n , g ;
struct node
{
  int n , m ;
  __int128 a[kMaxN][kMaxN] ;
  node(int x , int y)
  {
  	n = x ;
  	m = y ;
  	memset(a , 0 , sizeof a) ;
	}
	node operator * (const node b)
	{
		node 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] ;
					c.a[i][j] %= mod ;
				}
			}
		}
		return c ;
	}
};
node ksm(node a , ll b)
{
	node mul(2 , 2) ;
	for( int i = 1 ; i <= 2 ; i++ ) mul.a[i][i] = 1 ;
	while(b)
	{
		if(b & 1) mul = mul * a ;
		a = a * a ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void init()
{

}
void work()
{
  init() ;
  mod = read() , aa = read() , c = read() , x0 = read() , n = read() , g = read() ;
  node a(1 , 2) ;
  a.a[1][1] = x0 ;
  a.a[1][2] = c ;
	node b(2 , 2) ;
	b.a[1][1] = aa ;
	b.a[2][1] = 1 ;
	b.a[1][2] = 0 ;
	b.a[2][2] = 1 ;
	b = ksm(b , n) ;
	a = a * b ;
	write(a.a[1][1] % g) ;
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}

P3758 [TJOI2017] 可乐

floyd 加速板子。

发现现在的邻接表乘上自己,就是再走一次的邻接表,因此,这题加上一条自己和自己的边,就可以表示停留,设 i=0i = 0 表示已经爆炸,连接从 11 到 nn 的所有点到 00 的边,就可以表示爆炸。最后直接统计 11 的个数。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#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";\
}
using namespace std ;
const int kMaxN = 35 , MOD = 2017 , INF = 1e9 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
int n , m , t , ans ;
struct node
{
  int a[kMaxN][kMaxN] ;
  node()
  {
  	memset(a , 0 , sizeof a) ;
	}
	node operator * (const node b)
	{
		node 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 ;
	}
};
node ksm(node a , ll b)
{
	node 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 ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void init()
{

}
void work()
{
  init() ;
  cin >> n >> m ;
  node 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 = ksm(a , t) ;
  for( int i = 0 ; i <= n ; i++ )
  {
		ans += a.a[1][i] ;
		ans %= MOD ;
	}
  cout << ans ;
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}
原文