結果

問題 No.658 テトラナッチ数列 Hard
ユーザー kichirb3kichirb3
提出日時 2018-03-06 22:33:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 1,207 ms
コンパイル使用メモリ 111,312 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 21:02:28
合計ジャッジ時間 1,998 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// No.658 テトラナッチ数列 Hard
// https://yukicoder.me/problems/no/658
//

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <iomanip>
#include <sstream>
#include <numeric>
using namespace std;

pair<vector<int>, int> solve(long long limit);


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

    unsigned int Q;
    cin >> Q;
    vector<long long> queries(Q);
    for (auto i = 0; i < Q; ++i)
        cin >> queries[i];

    pair<vector<int>, int>res = solve(*max_element(queries.begin(), queries.end()));
    vector<int> tetra = res.first;
    int loop_start = res.second;
    vector<int> tetra_mod = tetra;
    for (auto i = 0; i < loop_start; ++i)
        tetra_mod.erase(tetra_mod.begin());

    int orikaeshi = tetra.size();
    int cycle = orikaeshi - loop_start;

    for (auto q: queries) {
        if (q >= orikaeshi)
            cout << tetra_mod[(q-loop_start)%cycle] << endl;
        else
            cout << tetra[q] << endl;
    }
}


pair<vector<int>, int> solve(long long limit) {
    vector<int> res {-1, 0, 0, 0, 1};
    unordered_map<string, int> loop_check;
    int loop_start = -1;

    for (auto i = 5; i <= limit; ++i ) {
        stringstream ss;
        ss << setw(2) << setfill('0') << res[i-4];
        ss << setw(2) << setfill('0') << res[i-3];
        ss << setw(2) << setfill('0') << res[i-2];
        ss << setw(2) << setfill('0') << res[i-1];
        // cout << ss.str() << endl;
        if (loop_check.find(ss.str()) != loop_check.end()) {
            loop_start = loop_check[ss.str()];
            break;
        }
        loop_check[ss.str()] = i;
        int tmp = accumulate(res.end()-4, res.end(), 0);
        res.push_back(tmp % 17);
    }
    return make_pair(res, loop_start);
}
0