Advertisement
den4ik2003

Untitled

Mar 22nd, 2022
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using std::vector;
  5.  
  6. bool Bit(int mask, int pos) {
  7.   return ((mask >> pos) & 1);
  8. }
  9.  
  10. bool CheckOK(int mask1, int mask2, int m) {
  11.   for (int i = 0; i < m - 1; ++i) {
  12.     if (Bit(mask1, i) == Bit(mask2, i) &&
  13.       Bit(mask1, i + 1) == Bit(mask2, i + 1) &&
  14.       Bit(mask1, i) == Bit(mask1, i + 1)) {
  15.       return false;
  16.     }
  17.   }
  18.   return true;
  19. }
  20.  
  21. vector<vector<int>> IsOK(int m) {
  22.   vector<vector<int>> is_ok(1 << m);
  23.   for (int left_mask = 0; left_mask < (1 << m); ++left_mask) {
  24.     vector<int> temp_is_ok(1 << m);
  25.     for (int right_mask = 0; right_mask < (1 << m); ++right_mask) {
  26.       temp_is_ok[right_mask] = CheckOK(left_mask, right_mask, m);
  27.     }
  28.     is_ok[left_mask] = temp_is_ok;
  29.   }
  30.   return is_ok;
  31. }
  32.  
  33. vector<vector<int>> MatrixMultiplication(vector<vector<int>> first_matrix,
  34.                                          vector<vector<int>> second_matrix, int gor_size, int mod, int m) {
  35.   int matrix_size = (1 << m);
  36.   vector<vector<int>> res_matrix(matrix_size, vector<int>(gor_size, 0));
  37.   for (int i = 0; i < matrix_size; ++i) {
  38.     for (int j = 0; j < gor_size; ++j) {
  39.       for (int k = 0; k < matrix_size; ++k) {
  40.         res_matrix[i][j] += ((long long)(first_matrix[i][k]) * (long long)(second_matrix[k][j])) % mod;
  41.         res_matrix[i][j] %= mod;
  42.       }
  43.     }
  44.   }
  45.   return res_matrix;
  46. }
  47.  
  48. vector<vector<int>> MatrixDegree(long long n, vector<vector<int>>& matrix, int mod, int m) {
  49.   int matrix_size = (1 << m);
  50.   if (n == 1) {
  51.     return matrix;
  52.   }
  53.   if (n % 2 == 0) {
  54.     vector<vector<int>> temp_matrix = MatrixDegree(n / 2, matrix, mod, m);
  55.     return MatrixMultiplication(temp_matrix, temp_matrix, matrix_size, mod, m);
  56.   }
  57.   return MatrixMultiplication(matrix, MatrixDegree(n - 1, matrix, mod, m), matrix_size, mod, m);
  58. }
  59.  
  60. int main() {
  61.   int T;
  62.   std::cin >> T;
  63.   while (T-- > 0) {
  64.     int m, mod;
  65.     long long n;
  66.     std::cin >> n >> m >> mod;
  67.     if (n == 1) {
  68.       std::cout << (1 << m) % mod << "\n";
  69.       continue;
  70.     }
  71.     vector<vector<int>> pretty_patterns_base((1 << m), vector<int>(1, 1));
  72.     vector<vector<int>> is_ok = IsOK(m);
  73.     vector<vector<int>>IsOKDegree = MatrixDegree(n - 1, is_ok, mod, m);
  74.     vector<vector<int>> pretty_pattern = MatrixMultiplication(IsOKDegree, pretty_patterns_base, 1, mod, m);
  75.     long long answer = 0;
  76.     for (int i = 0; i < (1 << m); ++i) {
  77.       answer += pretty_pattern[i][0];
  78.       answer %= mod;
  79.     }
  80.     std::cout << answer << "\n";
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement