hihocoder offer收割编程练习赛13 D 骑士游历

时间:2022-09-18 05:16:26

思路:

矩阵快速幂。

实现:

 #include <iostream>
#include <cstdio>
#include <vector>
using namespace std; typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat; const ll mod = 1e9 + ; ll n, x, y; mat mul(mat & a, mat & b)
{
mat c(a.size(), vec(b[].size()));
for (int i = ; i < a.size(); i++)
{
for (int k = ; k < b.size(); k++)
{
for (int j = ; j < b[].size(); j++)
{
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}
}
}
return c;
} mat pow(mat a, ll n)
{
mat b(a.size(), vec(a.size()));
for (int i = ; i < a.size(); i++)
{
b[i][i] = ;
}
while (n > )
{
if (n & )
b = mul(b, a);
a = mul(a, a);
n >>= ;
}
return b;
} bool check(int x, int y)
{
int m = x / , n = x % , p = y / , q = y % ;
if (abs(m - p) == && abs(n - q) == )
return true;
if (abs(m - p) == && abs(n - q) == )
return true;
return false;
} void init(mat & x, mat & y, int p, int q)
{
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (check(i, j))
x[i][j] = x[j][i] = ;
else
x[i][j] = x[j][i] = ;
}
}
for (int i = ; i < ; i++)
{
y[][i] = ;
}
y[][p * + q] = ;
} int main()
{
cin >> n >> x >> y;
x--, y--;
mat a(, vec());
mat m(, vec());
init(a, m, x, y);
a = pow(a, n);
m = mul(m, a);
ll cnt = ;
for (int i = ; i < ; i++)
{
cnt += m[][i];
cnt %= mod;
}
cout << cnt << endl;
return ;
}