結果
| 問題 |
No.2893 Minahoshi (Hard)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-07 00:21:38 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 51 ms / 2,000 ms |
| コード長 | 2,135 bytes |
| コンパイル時間 | 5,325 ms |
| コンパイル使用メモリ | 293,052 KB |
| 実行使用メモリ | 11,144 KB |
| 最終ジャッジ日時 | 2024-09-13 20:52:48 |
| 合計ジャッジ時間 | 9,579 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 49 |
ソースコード
//line 1 "answer.cpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const vector<int> ns = {1, 2, 5, 10, 19, 36, 69, 134, 263, 520, 1033, 2058, 4107, 8204, 16397, 32782, 65551, 131088, 262161};
const vector<vector<int>> primitive_polynomials = {
{},
{1},
{1, 2},
{1, 3},
{3, 4},
{3, 5},
{5, 6},
{6, 7},
{4, 5, 6, 8},
{5, 9},
{7, 10},
{9, 11},
{4, 10, 11, 12},
{8, 11, 12, 13},
{2, 12, 13, 14},
{14, 15},
{11, 13, 14, 16},
{14, 17},
{11, 18},
};
vector<pair<int, int>> lfsr(ll d) {
vector<int> p = primitive_polynomials[d];
ll x = (1 << d) - 1;
vector<int> r;
while (r.size() == 0 || r[0] != x) {
r.push_back(x);
x <<= 1;
for (auto k : p) if ((x >> k) & 1) x ^= 1;
if ((x >> d) & 1) x ^= (1 << d);
}
r.insert(find(r.begin(), r.end(), (1 << (d-1))) + 1, 0);
vector<pair<int, int>> result(1 << d);
for (size_t i=0; i < (1 << d); ++i) { result[r[i]] = {0, r[(i+1) % (r.size())]}; }
return result;
}
int solve(int n) {
if (n == 1) {cout << "a" << endl; return 0;}
int d = (upper_bound(ns.begin(), ns.end(), n) - ns.begin()) - 1;
vector<vector<pair<int, int>>> c(2);
c[0] = lfsr(d);
c[1] = vector<pair<int, int>>(1 << d, {-1, -1});
int l = ns[d];
int s = 0;
unordered_set<int> r;
for (int x=0; x < (1 << d); ++x) r.insert(x);
while (l < n) {
int x = *r.begin();
while (c[1][x].first == -1) {
r.erase(x);
l += 1;
c[1][x] = {1, c[0][x].second ^ 1};
x = c[1][x].second;
}
s = c[0][x].second;
swap(c[0][x], c[1][x]);
}
pair<int, int> pos = {0, s};
string ans;
for (int i=d-1; i >= 0; i--) ans += ((pos.second >> i) & 1) ? 'a' : 'b';
while (ans.size() < n) {
auto [t, x] = pos;
auto [tn, xn] = c[t][x];
ans += (xn & 1) ? 'a' : 'b';
pos = {tn, xn};
}
cout << ans << endl;
return 0;
}
int main() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
solve(n);
}
}