• 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 }
  • 相关阅读:
    JDBC访问数据库的基本步骤是什么?
    Java 为每个原始类型提供了哪些包装类型:
    JDBC访问数据库的基本步骤是什么?
    String 类的常用方法都有那些?
    面向对象三大特性
    三个与取整有关的方法:
    ==和equals方法究竟有什么区别?
    &和&&的区别?
    说一说Servlet的生命周期
    spring框架学习
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6690704.html
Copyright © 2020-2023  润新知