結果

問題 No.658 テトラナッチ数列 Hard
ユーザー tnakao0123tnakao0123
提出日時 2018-03-03 22:46:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 2,094 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 83,364 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 07:21:49
合計ジャッジ時間 1,414 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 19 ms
4,376 KB
testcase_05 AC 22 ms
4,376 KB
testcase_06 AC 28 ms
4,380 KB
testcase_07 AC 30 ms
4,376 KB
testcase_08 AC 34 ms
4,380 KB
testcase_09 AC 50 ms
4,384 KB
testcase_10 AC 50 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 658.cc: No.658 テトラナッチ数列 Hard - 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 = 4;
const int MOD = 17;
const int MAX_K = 60;

const int A[N][N] = {
  { 0, 1, 0, 0 },
  { 0, 0, 1, 0 },
  { 0, 0, 0, 1 },
  { 1, 1, 1, 1 }
};

const int V[N] = { 0, 0, 0, 1 };

/* typedef */

typedef int vec[N];
typedef vec mat[N];
typedef long long ll;

/* global variables */

mat as[MAX_K + 1];

/* subroutines */

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 addmat(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] = (a[i][j] + b[i][j]) % MOD;
}

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] % MOD;
      c[i][j] %= MOD;
    }
}

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] % MOD;
    c[i] %= MOD;
  }
}

/* main */

int main() {
  copymat(A, as[0]);
  for (int k = 0; k < MAX_K; k++) mulmat(as[k], as[k], as[k + 1]);

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

  while (q--) {
    ll n;
    scanf("%lld", &n);

    if (n <= N) printf("%d\n", V[n - 1]);
    else {
      n -= N;
      mat s;
      unitmat(s);
      for (int k = 0; n > 0LL; k++, n >>= 1)
	if ((n & 1LL) != 0LL) {
	  mat t;
	  mulmat(s, as[k], t);
	  copymat(t, s);
	}
      
      vec v;
      mulmatvec(s, V, v);
      printf("%d\n", v[N - 1]);
    }
  }
  return 0;
}
0