// No.658 テトラナッチ数列 Hard // https://yukicoder.me/problems/no/658 // #include #include #include #include #include #include #include #include #include using namespace std; pair, int> solve(long long limit); int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); unsigned int Q; cin >> Q; vector queries(Q); for (auto i = 0; i < Q; ++i) cin >> queries[i]; pair, int>res = solve(*max_element(queries.begin(), queries.end())); vector tetra = res.first; int loop_start = res.second; vector 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, int> solve(long long limit) { vector res {-1, 0, 0, 0, 1}; unordered_map 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); }