結果
| 問題 |
No.1704 Many Bus Stops (easy)
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2021-10-09 03:07:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 339 ms / 2,000 ms |
| コード長 | 2,988 bytes |
| コンパイル時間 | 930 ms |
| コンパイル使用メモリ | 97,916 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-23 12:52:43 |
| 合計ジャッジ時間 | 11,660 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 41 |
ソースコード
/* -*- 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;
}
tnakao0123