結果

問題 No.1743 Permutation Code
ユーザー harurun
提出日時 2026-07-09 04:25:13
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 5,271 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,389 ms
コンパイル使用メモリ 229,892 KB
実行使用メモリ 9,048 KB
最終ジャッジ日時 2026-07-09 04:25:35
合計ジャッジ時間 12,706 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

long long total_len(long long n) {
    long long res = 0;

    for (long long len = 1, l = 1; l <= n; len++) {
        long long r = min(n, l * 2 - 1);
        res += len * (r - l + 1);

        if (l > n / 2) break;
        l <<= 1;
    }

    return res;
}

int bit_length(int x) {
    return 32 - __builtin_clz((unsigned)x);
}

struct DLX {
    int nCol;

    vector<int> L, R, U, D;
    vector<int> col, row, sz;
    vector<int> answer;

    DLX(int nCol, long long reserveNodes = 0) : nCol(nCol) {
        int base = nCol + 1;
        int cap = base + (int)min<long long>(reserveNodes, INT_MAX - base - 1);

        L.reserve(cap);
        R.reserve(cap);
        U.reserve(cap);
        D.reserve(cap);
        col.reserve(cap);
        row.reserve(cap);

        L.resize(nCol + 1);
        R.resize(nCol + 1);
        U.resize(nCol + 1);
        D.resize(nCol + 1);
        col.resize(nCol + 1);
        row.resize(nCol + 1, -1);
        sz.assign(nCol + 1, 0);

        for (int i = 0; i <= nCol; i++) {
            L[i] = i - 1;
            R[i] = i + 1;
            U[i] = D[i] = i;
            col[i] = i;
        }

        L[0] = nCol;
        R[nCol] = 0;
    }

    void add_node(int r, int c, int& first) {
        int id = (int)col.size();

        L.push_back(id);
        R.push_back(id);
        U.push_back(0);
        D.push_back(0);
        col.push_back(c);
        row.push_back(r);

        U[id] = U[c];
        D[id] = c;
        D[U[c]] = id;
        U[c] = id;
        sz[c]++;

        if (first == -1) {
            first = id;
            L[id] = R[id] = id;
        } else {
            R[id] = first;
            L[id] = L[first];
            R[L[first]] = id;
            L[first] = id;
        }
    }

    void add_row(int r, const vector<int>& cols) {
        int first = -1;

        for (int c : cols) {
            add_node(r, c, first);
        }
    }

    void cover(int c) {
        R[L[c]] = R[c];
        L[R[c]] = L[c];

        for (int i = D[c]; i != c; i = D[i]) {
            for (int j = R[i]; j != i; j = R[j]) {
                D[U[j]] = D[j];
                U[D[j]] = U[j];
                sz[col[j]]--;
            }
        }
    }

    void uncover(int c) {
        for (int i = U[c]; i != c; i = U[i]) {
            for (int j = L[i]; j != i; j = L[j]) {
                sz[col[j]]++;
                D[U[j]] = j;
                U[D[j]] = j;
            }
        }

        R[L[c]] = c;
        L[R[c]] = c;
    }

    bool solve() {
        if (R[0] == 0) return true;

        int c = R[0];

        for (int j = R[c]; j != 0; j = R[j]) {
            if (sz[j] < sz[c]) {
                c = j;
            }
        }

        if (sz[c] == 0) return false;

        cover(c);

        for (int i = D[c]; i != c; i = D[i]) {
            answer.push_back(row[i]);

            for (int j = R[i]; j != i; j = R[j]) {
                cover(col[j]);
            }

            if (solve()) return true;

            for (int j = L[i]; j != i; j = L[j]) {
                uncover(col[j]);
            }

            answer.pop_back();
        }

        uncover(c);
        return false;
    }
};

struct RowInfo {
    int start;
    int len;
    int value;
};

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

    string C;
    cin >> C;

    int S = (int)C.size();

    long long lo = 1, hi = 1;

    while (total_len(hi) < S) {
        hi <<= 1;
    }

    while (lo < hi) {
        long long mid = (lo + hi) / 2;

        if (total_len(mid) >= S) hi = mid;
        else lo = mid + 1;
    }

    if (total_len(lo) != S) {
        return 0;
    }

    int N = (int)lo;
    int M = bit_length(N);

    long long nodeCount = 0;

    for (int s = 0; s < S; s++) {
        if (C[s] != '1') continue;

        int v = 0;

        for (int len = 1; len <= M && s + len <= S; len++) {
            v = (v << 1) + (C[s + len - 1] - '0');

            if (v > N) break;

            nodeCount += len + 1;
        }
    }

    // 列:
    // 1..N       : 数字
    // N+1..N+S  : 文字位置
    DLX dlx(N + S, nodeCount);

    vector<RowInfo> rows;

    for (int s = 0; s < S; s++) {
        if (C[s] != '1') continue;

        int v = 0;
        vector<int> cols;
        cols.reserve(M + 1);

        for (int len = 1; len <= M && s + len <= S; len++) {
            v = (v << 1) + (C[s + len - 1] - '0');

            if (v > N) break;

            cols.clear();

            // 数字 v を使う
            cols.push_back(v);

            // 文字位置 s..s+len-1 を覆う
            for (int p = s; p < s + len; p++) {
                cols.push_back(N + p + 1);
            }

            int id = (int)rows.size();
            rows.push_back({s, len, v});
            dlx.add_row(id, cols);
        }
    }

    if (!dlx.solve()) {
        return 0;
    }

    vector<RowInfo> chosen;

    for (int id : dlx.answer) {
        chosen.push_back(rows[id]);
    }

    sort(chosen.begin(), chosen.end(), [](const RowInfo& a, const RowInfo& b) {
        return a.start < b.start;
    });

    for (int i = 0; i < (int)chosen.size(); i++) {
        if (i) cout << ' ';
        cout << chosen[i].value;
    }

    cout << '\n';

    return 0;
}
0