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

Posted 王宜鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hihocoder offer收割编程练习赛13 D 骑士游历相关的知识,希望对你有一定的参考价值。

思路:

矩阵快速幂。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 typedef vector<ll> vec;
 8 typedef vector<vec> mat;
 9 
10 const ll mod = 1e9 + 7;
11 
12 ll n, x, y;
13 
14 mat mul(mat & a, mat & b)
15 {
16     mat c(a.size(), vec(b[0].size()));
17     for (int i = 0; i < a.size(); i++)
18     {
19         for (int k = 0; k < b.size(); k++)
20         {
21             for (int j = 0; j < b[0].size(); j++)
22             {
23                 c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
24             }
25         }
26     }
27     return c;
28 }
29 
30 mat pow(mat a, ll n)
31 {
32     mat b(a.size(), vec(a.size()));
33     for (int i = 0; i < a.size(); i++)
34     {
35         b[i][i] = 1;
36     }
37     while (n > 0)
38     {
39         if (n & 1)
40             b = mul(b, a);
41         a = mul(a, a);
42         n >>= 1;
43     }
44     return b;
45 }
46 
47 bool check(int x, int y)
48 {
49     int m = x / 8, n = x % 8, p = y / 8, q = y % 8;
50     if (abs(m - p) == 1 && abs(n - q) == 2)
51         return true;
52     if (abs(m - p) == 2 && abs(n - q) == 1)
53         return true;
54     return false;
55 }
56 
57 void init(mat & x, mat & y, int p, int q)
58 {
59     for (int i = 0; i < 64; i++)
60     {
61         for (int j = 0; j < 64; j++)
62         {
63             if (check(i, j))
64                 x[i][j] = x[j][i] = 1;
65             else
66                 x[i][j] = x[j][i] = 0;
67         }
68     }
69     for (int i = 0; i < 64; i++)
70     {
71         y[0][i] = 0;
72     }
73     y[0][p * 8 + q] = 1;
74 }
75 
76 int main()
77 {
78     cin >> n >> x >> y;
79     x--, y--;
80     mat a(64, vec(64));
81     mat m(1, vec(64));
82     init(a, m, x, y);
83     a = pow(a, n);
84     m = mul(m, a);
85     ll cnt = 0;
86     for (int i = 0; i < 64; i++)
87     {
88         cnt += m[0][i];
89         cnt %= mod;
90     }
91     cout << cnt << endl;
92     return 0;
93 }

 

以上是关于hihocoder offer收割编程练习赛13 D 骑士游历的主要内容,如果未能解决你的问题,请参考以下文章

hihocoder offer收割编程练习赛12 D 寻找最大值

hihocoder - [Offer收割]编程练习赛17

[hihocoder][Offer收割]编程练习赛64

[hihocoder] [Offer收割]编程练习赛43

[hihocoder][Offer收割]编程练习赛58

hihocoder offer收割编程练习赛8 A