[SDOI2009] HH去散步 题解

[SDOI2009] HH去散步 题解

[SDOI2009] HH去散步

d p i j dp_{i_j} dpij 表示第 i i i 个时刻,走到第 j j j 条边的终点的方案数

转移:从当前点 u u u 开始,枚举从 u u u 出发的所有点,向 d p i + 1 , t o u dp_{i+1,to_u} dpi+1,tou 转移

但是 t t t 非常大,所以需要使用矩阵快速幂

设初始矩阵为 A \text{A} A,转移矩阵为 B \text{B} B,那么答案矩阵 C = A × B t − 1 \text{C} = \text{A}\times\text{B}^{t-1} C=A×Bt1

// Problem: P2151 [SDOI2009] HH去散步
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2151
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// https://codeforces.com/problemset/customtest

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
# define int long long
# define rd(t) read <t> ()
# define mem(a, b) memset (a, b, sizeof (a))
# define fi first
# define se second
# define lc u << 1
# define rc u << 1 | 1
# define debug printf ("debug\n")
const int N = 155, M = N, mod = 45989; 
template <typename T> inline T read ()
{
    T s = 0; int w = 1; char c = getchar (); 
    for (; !isdigit (c); c = getchar ()) { if (c == '-') w = -1; }
    for (; isdigit (c); c = getchar ()) s = (s << 1) + (s << 3) + c - '0';
    return s * w;
}

int n, m, t, A, B; 
struct edge { int vtx, nxt; } e[M]; int h[N], idx; 
void add (int u, int v) { e[ ++ idx] = {v, h[u]}; h[u] = idx; } 
struct matrix
{
	int a[N][N]; 
	matrix () { mem (a, 0); } 
	matrix operator * (const matrix &b) const
	{
		matrix ans; 
		for (int i = 1; i < N; i ++ )
		{
			for (int k = 1; k < N; k ++ )
			{
				for (int j = 1; j < N; j ++ )
					ans.a[i][j] = (ans.a[i][j] + a[i][k] * b.a[k][j]) % mod; 
            }
        }
        return ans; 
    }
} a, b; 
matrix quick_pow (matrix ans, matrix a, int b)
{
	while (b)
	{
		if (b & 1) ans = ans * a; 
		a = a * a; 
		b >>= 1; 
    }
    return ans; 
}
int rev (int x) { return (x % 2 == 0 ? x - 1 : x + 1); }
signed main ()
{
	mem (h, -1); 
	n = rd (int), m = rd (int), t = rd (int); 
	A = rd (int) + 1, B = rd (int) + 1; 
	for (int i = 1; i <= m; i ++ )
	{
		int u = rd (int) + 1, v = rd (int) + 1; 
		add (u, v), add (v, u); 
	} 
	for (int i = 1; i <= idx; i ++ )
	{
		int u = e[i].vtx; 
		for (int j = h[u]; ~j; j = e[j].nxt)
		{
			if (i == rev (j)) continue; 
			b.a[i][j] ++ ; 
		}
    }
    for (int i = h[A]; ~i; i = e[i].nxt) a.a[1][i] ++ ; 
    matrix ans = quick_pow (a, b, t - 1); 
    int res = 0; 
    for (int i = h[B]; ~i; i = e[i].nxt)
    	res = (res + ans.a[1][rev (i)]) % mod; 
    printf ("%lld\n", res); 
    return 0; 
}

  • 15
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值