码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-10 21:49:00      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:main   pre   amp   else   编程   pow   while   als   size   

思路:

矩阵快速幂。

实现:

 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 骑士游历

标签:main   pre   amp   else   编程   pow   while   als   size   

原文地址:http://www.cnblogs.com/wangyiming/p/6690704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!