結果

問題 No.2893 Minahoshi (Hard)
ユーザー 👑 seekworser
提出日時 2024-09-07 00:19:28
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,047 bytes
コンパイル時間 3,390 ms
コンパイル使用メモリ 265,476 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-13 20:52:41
合計ジャッジ時間 11,351 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 main() {
    int n; cin >> 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';
    cout << "ans: " << ans << endl;
    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;
}
0