結果

問題 No.1704 Many Bus Stops (easy)
ユーザー simansiman
提出日時 2022-01-24 09:05:42
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 1,802 bytes
コンパイル時間 6,222 ms
コンパイル使用メモリ 105,692 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 18:52:06
合計ジャッジ時間 13,307 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 273 ms
4,376 KB
testcase_02 AC 75 ms
4,380 KB
testcase_03 AC 76 ms
4,376 KB
testcase_04 AC 75 ms
4,376 KB
testcase_05 AC 75 ms
4,380 KB
testcase_06 AC 75 ms
4,380 KB
testcase_07 AC 76 ms
4,380 KB
testcase_08 AC 76 ms
4,376 KB
testcase_09 AC 76 ms
4,380 KB
testcase_10 AC 75 ms
4,380 KB
testcase_11 AC 77 ms
4,380 KB
testcase_12 AC 75 ms
4,376 KB
testcase_13 AC 75 ms
4,380 KB
testcase_14 AC 75 ms
4,380 KB
testcase_15 AC 75 ms
4,380 KB
testcase_16 AC 75 ms
4,376 KB
testcase_17 AC 77 ms
4,380 KB
testcase_18 AC 77 ms
4,384 KB
testcase_19 AC 76 ms
4,380 KB
testcase_20 AC 76 ms
4,376 KB
testcase_21 AC 77 ms
4,380 KB
testcase_22 AC 274 ms
4,384 KB
testcase_23 AC 276 ms
4,376 KB
testcase_24 AC 273 ms
4,380 KB
testcase_25 AC 279 ms
4,380 KB
testcase_26 AC 273 ms
4,376 KB
testcase_27 AC 272 ms
4,380 KB
testcase_28 AC 272 ms
4,380 KB
testcase_29 AC 273 ms
4,376 KB
testcase_30 AC 277 ms
4,380 KB
testcase_31 AC 271 ms
4,376 KB
testcase_32 AC 272 ms
4,380 KB
testcase_33 AC 273 ms
4,384 KB
testcase_34 AC 273 ms
4,376 KB
testcase_35 AC 273 ms
4,384 KB
testcase_36 AC 272 ms
4,380 KB
testcase_37 AC 272 ms
4,380 KB
testcase_38 AC 273 ms
4,376 KB
testcase_39 AC 275 ms
4,376 KB
testcase_40 AC 274 ms
4,376 KB
testcase_41 AC 277 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector <ll> vec;
typedef vector <vec> mat;

ll N, K;
ll MOD = 1'000'000'007;

ll mod_pow(ll x, ll n, ll mod = MOD) {
  ll res = 1;

  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }

    x = x * x % mod;
    n >>= 1;
  }

  return res;
}

ll mod_inverse(ll x, ll mod = MOD) {
  return mod_pow(x, mod - 2, mod);
}

mat mul(mat &A, mat &B) {
  mat C(A.size(), vec(B[0].size()));

  for (int i = 0; i < A.size(); ++i) {
    for (int k = 0; k < B.size(); ++k) {
      for (int j = 0; j < B[0].size(); ++j) {
        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
      }
    }
  }

  return C;
}

// 行列の累乗
mat pow(mat A, ll n) {
  mat B(A.size(), vec(A.size()));

  for (int i = 0; i < A.size(); ++i) {
    B[i][i] = 1;
  }
  while (n > 0) {
    if (n & 1) B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }

  return B;
}

ll _A[6][6] = {
  {1, 0, 0, 0, 1, 1},
  {0, 1, 0, 1, 0, 1}, 
  {0, 0, 1, 1, 1, 0}, 
  {3, 0, 0, 0, 0, 0}, 
  {0, 3, 0, 0, 0, 0}, 
  {0, 0, 3, 0, 0, 0}, 
};

ll _B[6][1] = {
  {1},
  {0},
  {0},
  {3},
  {0},
  {0},
};

int main() {
  int T;
  cin >> T;

  mat A(6);

  for (int i = 0; i < 6; ++i) {
    vector<ll> row;
    for (int j = 0; j < 6; ++j) {
      row.push_back(_A[i][j]);
    }
    A[i] = row;
  }

  mat B(6);
  for (int i = 0; i < 6; ++i) {
    B[i].push_back(_B[i][0]);
  }

  for (int i = 0; i < T; ++i){
    int N;
    cin >> N;
    mat C = pow(A, N);
    ll m = mod_inverse(mod_pow(3, N + 1, MOD));
    mat D = mul(C, B);
    cout << (m * D[3][0]) % MOD << endl;
  }

  return 0;
}

0