結果

問題 No.1704 Many Bus Stops (easy)
ユーザー tnakao0123tnakao0123
提出日時 2021-10-09 03:07:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 2,988 bytes
コンパイル時間 784 ms
コンパイル使用メモリ 93,684 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 19:07:36
合計ジャッジ時間 11,616 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 329 ms
4,380 KB
testcase_02 AC 108 ms
4,376 KB
testcase_03 AC 108 ms
4,380 KB
testcase_04 AC 108 ms
4,380 KB
testcase_05 AC 109 ms
4,376 KB
testcase_06 AC 109 ms
4,376 KB
testcase_07 AC 108 ms
4,376 KB
testcase_08 AC 109 ms
4,376 KB
testcase_09 AC 109 ms
4,376 KB
testcase_10 AC 108 ms
4,380 KB
testcase_11 AC 109 ms
4,376 KB
testcase_12 AC 109 ms
4,380 KB
testcase_13 AC 108 ms
4,376 KB
testcase_14 AC 109 ms
4,380 KB
testcase_15 AC 109 ms
4,376 KB
testcase_16 AC 109 ms
4,380 KB
testcase_17 AC 109 ms
4,376 KB
testcase_18 AC 108 ms
4,376 KB
testcase_19 AC 109 ms
4,380 KB
testcase_20 AC 108 ms
4,380 KB
testcase_21 AC 110 ms
4,376 KB
testcase_22 AC 335 ms
4,380 KB
testcase_23 AC 335 ms
4,380 KB
testcase_24 AC 336 ms
4,376 KB
testcase_25 AC 336 ms
4,376 KB
testcase_26 AC 337 ms
4,380 KB
testcase_27 AC 340 ms
4,380 KB
testcase_28 AC 335 ms
4,380 KB
testcase_29 AC 336 ms
4,380 KB
testcase_30 AC 335 ms
4,376 KB
testcase_31 AC 337 ms
4,380 KB
testcase_32 AC 335 ms
4,380 KB
testcase_33 AC 335 ms
4,380 KB
testcase_34 AC 336 ms
4,376 KB
testcase_35 AC 335 ms
4,376 KB
testcase_36 AC 336 ms
4,376 KB
testcase_37 AC 335 ms
4,376 KB
testcase_38 AC 336 ms
4,376 KB
testcase_39 AC 335 ms
4,376 KB
testcase_40 AC 335 ms
4,376 KB
testcase_41 AC 335 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1704.cc:  No.1704 Many Bus Stops (easy) - 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 */

const int N = 9;
const int MOD = 1000000007;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI((v + m.v) % MOD); }
  MI operator-(const MI m) const { return MI((v + MOD - m.v) % MOD); }
  MI operator*(const MI m) const { return MI((long long)v * m.v % MOD); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

typedef mi vec[N];
typedef vec mat[N];

/* global variables */

/* subroutines */

inline void initvec(vec a) { memset(a, 0, sizeof(vec)); }
inline void initmat(mat a) { memset(a, 0, sizeof(mat)); }

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

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

inline void mulmat(const mat a, const mat b, mat 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];
    }
}

inline void powmat(const mat a, int b, mat c) {
  mat s, t;
  copymat(a, s);
  unitmat(c);

  while (b > 0) {
    if (b & 1) {
      mulmat(c, s, t);
      copymat(t, c);
    }

    mulmat(s, s, t);
    copymat(t, s);
    b >>= 1;
  }
}

inline void mulmatvec(const mat a, const vec b, vec c) {
  for (int i = 0; i < N; i++) {
    c[i] = 0;
    for (int j = 0; j < N; j++) c[i] += a[i][j] * b[j];
  }
}

/* main */

int main() {
  mi inv3 = mi(3).inv();

  // vec = A, A->B, A->C, B->A, B, B->C, C->A, C->B, C
  mat ma = {
    { inv3, 0, 0, 1, 0, 0, 1, 0, 0 },
    { inv3, 0, 0, 0, 0, 0, 0, 0, 0 },
    { inv3, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, inv3, 0, 0, 0, 0 },
    { 0, 1, 0, 0, inv3, 0, 0, 1, 0 },
    { 0, 0, 0, 0, inv3, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, inv3 },
    { 0, 0, 0, 0, 0, 0, 0, 0, inv3 },
    { 0, 0, 1, 0, 0, 1, 0, 0, inv3 },
  };

  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int n;
    scanf("%d", &n);

    mat mb;
    powmat(ma, n, mb);

    printf("%d\n", mb[0][0].v);
  }
  return 0;
}
0