結果

問題 No.658 テトラナッチ数列 Hard
ユーザー tnakao0123
提出日時 2018-03-03 22:46:28
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 2,094 bytes
コンパイル時間 773 ms
コンパイル使用メモリ 85,876 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-04 23:32:01
合計ジャッジ時間 1,649 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:92:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   92 |   scanf("%d", &q);
      |   ~~~~~^~~~~~~~~~
main.cpp:96:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   96 |     scanf("%lld", &n);
      |     ~~~~~^~~~~~~~~~~~

ソースコード

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