結果

問題 No.658 テトラナッチ数列 Hard
ユーザー とばりとばり
提出日時 2018-09-07 17:23:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,164 bytes
コンパイル時間 1,684 ms
コンパイル使用メモリ 184,520 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-05 21:15:17
合計ジャッジ時間 2,705 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using int64 = long long;
using uint64 = unsigned long long;

using T = tuple<int, int, int, int>;    // (k-4, k-3, k-2, k-1)

vector<int> seq;
int cycleStart = 0, cycleLength = 0;

void buildSeq()
{
    map<T, int> recorder;   // Tが初めて出現した添字を記録する
    T t{0, 0, 0, 1};
    seq = vector<int>{0, 0, 0, 1};

    while (recorder.count(t) == 0)
    {
        int T1, T2, T3, T4, T5;
        tie(T1, T2, T3, T4) = t;

        T5 = (T1 + T2 + T3 + T4) % 17;
        T1 = T2; T2 = T3; T3 = T4; T4 = T5;

        recorder[t] = seq.size();
        seq.push_back(T5);
        t = make_tuple(T1, T2, T3, T4);
    }
    cycleStart = recorder[t];
    cycleLength = seq.size() - cycleStart;
}

int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    buildSeq();

    int Q;
    cin >> Q;

    for (int i = 0; i < Q; i++)
    {
        int64 n;
        cin >> n; n--;

        if (n < cycleStart)
        {
            cout << seq[n] << endl;
        }
        else
        {
            cout << seq[cycleStart + (n - cycleStart) % cycleLength] << endl;
        }
    }

    return 0;
}
0