結果
| 問題 |
No.658 テトラナッチ数列 Hard
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-03-03 16:53:33 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 696 ms / 2,000 ms |
| コード長 | 1,942 bytes |
| コンパイル時間 | 653 ms |
| コンパイル使用メモリ | 99,008 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-13 00:04:19 |
| 合計ジャッジ時間 | 4,211 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int[][] A = [[1,1,1,1], [1,0,0,0], [0,1,0,0], [0,0,1,0]];
int q;
scan(q);
while (q--) {
long n;
scan(n);
n--;
auto res = mul(powMat(A, n), [[1],[0],[0],[0]]);
writeln(res[3][0]);
}
}
int[][] powMat(int[][] A, long x) {
if (x > 0) {
auto res = square(powMat(A, x>>1));
if (x & 1) {
res = mul(res, A);
}
return res;
}
else {
auto res = new int[][](A.length, A.length);
foreach (i ; 0 .. A.length) res[i][i] = 1;
return res;
}
}
int[][] square(int[][] A) {
assert(A[0].length == A.length);
auto B = new int[][](A.length, A.length);
foreach (i ; 0 .. A.length) {
foreach (j ; 0 .. A.length) {
foreach (k ; 0 .. A.length) {
B[i][j] += A[i][k] * A[k][j];
B[i][j] %= 17;
}
}
}
return B;
}
auto mul(int[][] A, int[][] B) {
assert(A[0].length == B.length);
auto N = A.length;
auto M = B[0].length;
auto C = new int[][](N, M);
foreach (i ; 0 .. N) {
foreach (j ; 0 .. M) {
foreach (k ; 0 .. A[i].length) {
C[i][j] += A[i][k] * B[k][j];
C[i][j] %= 17;
}
}
}
return C;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}