結果

問題 No.569 3 x N グリッドのパスの数
ユーザー tnakao0123tnakao0123
提出日時 2017-09-21 19:53:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,158 bytes
コンパイル時間 625 ms
コンパイル使用メモリ 86,284 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 06:37:15
合計ジャッジ時間 2,218 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 2 ms
6,940 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 2 ms
6,944 KB
testcase_49 AC 2 ms
6,940 KB
testcase_50 AC 2 ms
6,944 KB
testcase_51 AC 2 ms
6,944 KB
testcase_52 AC 2 ms
6,940 KB
testcase_53 AC 2 ms
6,944 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 3 ms
6,944 KB
testcase_56 AC 2 ms
6,940 KB
testcase_57 AC 2 ms
6,944 KB
testcase_58 AC 2 ms
6,944 KB
testcase_59 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 569.cc: No.569 3 x N グリッドのパスの数 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

typedef long long ll;
const ll MOD = 1000000007;

// pattern:
// 0: 1000, 1: 0100, 2: 0010, 3: 0001,
// 4: 1220, 5: 1202, 6: 1022,
// 7: 0122,
// 8: 2210,
// 9: 2201, 10: 2021, 11: 0221

const int N = 12;
typedef ll mat[N][N];

const mat IM = {
  { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
  { 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0 },
  { 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
  { 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 },
  { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 },
  { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0 },
  { 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 },
  { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
};

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;

/* global variables */

/* subroutines */

void mulmat(const mat a, const mat b, mat c) { // a * b => c
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      c[i][j] = 0;
      for (int k = 0; k < N; k++) {
	c[i][j] += a[i][k] * b[k][j];
	c[i][j] %= MOD;
      }
    }
}

inline void initmat(mat a) {
  for (int i = 0; i < N; i++) {
    fill(a[i], a[i] + N, 0);
    a[i][i] = 1;
  }
}

inline void copymat(const mat a, mat b) { // a -> b
  memcpy(b, a, sizeof(mat));
}

void powmat(const mat a, ll b, mat p) { // a^b => c
  mat aa, c;
  initmat(p);
  copymat(a, aa);

  while (b > 0LL) {
    if ((b & 1LL) != 0LL) {
      mulmat(p, aa, c);
      copymat(c, p);
    }

    mulmat(aa, aa, c);
    copymat(c, aa);
    b >>= 1;
  }
}

/* main */

int main() {
  ll n;
  cin >> n;

  mat p;
  powmat(IM, n + 1, p);

  printf("%lld\n", p[3][0]);
  return 0;
}
0