Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define TRACE(x) x
  5. #define WATCH(x) TRACE(cout << #x" = " << x << endl)
  6. #define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
  7. #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})
  8.  
  9. #define all(x) (x).begin(), (x).end()
  10.  
  11. typedef long long ll;
  12. typedef vector<bool> vb;
  13. typedef vector<int> vi;
  14. typedef vector<vi> vvi;
  15. typedef vector<ll> vll;
  16. typedef vector<vll> vvll;
  17.  
  18. struct mll {
  19.     static const int MOD = 1e9 + 9;
  20.     static vector<mll> inv_cache;
  21.     static vector<mll> pow2, ipow2, fact, ifact;
  22.  
  23.     ll val;
  24.     mll(ll _val = 0) {
  25.         val = _val % MOD;
  26.         if (val < 0) val += MOD;
  27.     }
  28.  
  29.     #define def(op) \
  30.         mll operator op (const mll &o) const { \
  31.             return mll(val op o.val); \
  32.         }
  33.     def(+) def(-) def(*)
  34.  
  35.     mll pow(ll e) const {
  36.         if (e == 0) return mll(1);
  37.         if (e & 1) return *this * this->pow(e - 1);
  38.         return (*this * *this).pow(e/2);
  39.     }
  40.  
  41.     mll inv() const {
  42.         assert(val != 0);
  43.         if (val < inv_cache.size())
  44.             return inv_cache[val];
  45.         return this->pow(MOD - 2);
  46.     }
  47.  
  48.     mll operator / (const mll &o) const {
  49.         return *this * o.inv();
  50.     }
  51.  
  52.     static mll ncr(int n, int r) {
  53.         if (r < 0 || r > n) return mll(0);
  54.         return fact[n] * ifact[r] * ifact[n-r];
  55.     }
  56.  
  57.     static void init(int mval) {
  58.         inv_cache.resize(mval);
  59.         inv_cache[1] = mll(1);
  60.         for (int i = 2; i < mval; i++)
  61.             inv_cache[i] = inv_cache[MOD % i] * mll(-(MOD/i));
  62.  
  63.         pow2.resize(mval), ipow2.resize(mval);
  64.         pow2[0] = ipow2[0] = mll(0);
  65.         fact.resize(mval), ifact.resize(mval);
  66.         fact[0] = ifact[0] = mll(1);
  67.  
  68.         for (int i = 1; i < mval; i++) {
  69.             pow2[i] = pow2[i-1] * 2;
  70.             ipow2[i] = ipow2[i-1] * inv_cache[2];
  71.             fact[i] = fact[i-1] * i;
  72.             ifact[i] = ifact[i-1] * inv_cache[i];
  73.         }
  74.     }
  75.  
  76.     friend ostream& operator<<(ostream &os, mll m) {
  77.         return os << m.val;
  78.     }
  79. };
  80. vector<mll> mll::inv_cache;
  81. vector<mll> mll::pow2, mll::ipow2, mll::fact, mll::ifact;
  82.  
  83. template<typename T> struct matrix {
  84.     int N;
  85.     vector<T> dat;
  86.  
  87.     matrix<T> (int _N, T fill = T(0), T diag = T(0)) {
  88.         N = _N;
  89.         dat.resize(N * N, fill);
  90.  
  91.         for (int i = 0; i < N; i++)
  92.             (*this)(i, i) = diag;
  93.     }
  94.  
  95.     T& operator()(int i, int j) {
  96.         return dat[N * i + j];
  97.     }
  98.  
  99.     matrix<T> operator *(matrix<T> &b){
  100.         matrix<T> r(N);
  101.  
  102.         for(int i=0; i<N; i++)
  103.             for(int j=0; j<N; j++)
  104.                 for(int k=0; k<N; k++)
  105.                     r(i, j) = r(i, j) + (*this)(i, k) * b(k, j);
  106.  
  107.         return r;
  108.     }
  109.  
  110.     matrix<T> pow(ll expo){
  111.         if(!expo) return matrix<T>(N, T(0), T(1));
  112.         matrix<T> r = (*this * *this).pow(expo/2);
  113.         return expo&1 ? r * *this : r;
  114.     }
  115.  
  116.     friend ostream& operator<<(ostream &os, matrix<T> &m){
  117.         os << "{";
  118.         for(int i=0; i<m.N; i++){
  119.             if(i) os << "},\n ";
  120.             os << "{";
  121.             for(int j=0; j<m.N; j++){
  122.                 if(j) os << ", ";
  123.                 os << setw(10) << m(i, j) << setw(0);
  124.             }
  125.         }
  126.         return os << "}}";
  127.     }
  128. };
  129.  
  130. vector<vector<bool>> read(int M, int state) {
  131.     vector<vector<bool>> board(M, vector<bool>(2, false));
  132.     for (int i = 0; i < M; i++) {
  133.         for (int j = 0; j < 2; j++)
  134.             board[i][j] = (state >> (2 * i + j))&1;
  135.     }
  136.     return board;
  137. }
  138.  
  139. const int moves[8][2] = { { 1, 2 }, {1, -2}, {-1, 2}, {-1, -2},
  140.                           { 2, 1 }, {2, -1}, {-2, 1}, {-2, -1} };
  141.  
  142. bool valid(int M, vector<vector<bool>> &board) {
  143.     for (int i = 0; i < M; i++) {
  144.         for (int j = 0; j < 3; j++) {
  145.             if (!board[i][j]) continue;
  146.             for (int m = 0; m < 8; m++) {
  147.                 int ii = i + moves[m][0];
  148.                 int jj = j + moves[m][1];
  149.                 if (ii < 0 || jj < 0 || ii >= M || jj >= 3) continue;
  150.                 if (board[ii][jj]) return false;
  151.             }
  152.         }
  153.     }
  154.     return true;
  155. }
  156.  
  157. bool adj(int M, int s1, int s2) {
  158.     auto b1 = read(M, s1);
  159.     auto b2 = read(M, s2);
  160.  
  161.     for (int i = 0; i < M; i++) {
  162.         if (b2[i][0] != b1[i][1])
  163.             return false;
  164.     }
  165.  
  166.     vector<vector<bool>> comb(M, vector<bool>(3));
  167.     for (int i = 0; i < M; i++) {
  168.         comb[i][0] = b1[i][0];
  169.         comb[i][1] = b1[i][1];
  170.         comb[i][2] = b2[i][1];
  171.     }
  172.    
  173.     return valid(M, comb);
  174. }
  175.  
  176. int main() {
  177.     ios_base::sync_with_stdio(false);
  178.     cin.tie(0), cout.tie(0);
  179.     cout << fixed << setprecision(15);
  180.  
  181.     int T;
  182.     cin >> T;
  183.  
  184.     for (int t = 0; t < T; t++) {
  185.         int M, N;
  186.         cin >> M >> N;
  187.  
  188.         int states = 1 << (2 * M);
  189.  
  190.         matrix<mll> m(states, 0, 0);
  191.  
  192.         for (int s1 = 0; s1 < states; s1++) {
  193.             for (int s2 = 0; s2 < states; s2++) {
  194.                 if (adj(M, s1, s2))
  195.                     m(s1, s2) = m(s1, s2) + 1;
  196.             }
  197.         }
  198.  
  199.         m = m.pow(N + 2);
  200.         cout << m(0, 0) << "\n";
  201.     }
  202.  
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement