結果

問題 No.3408 1215 Segments
コンテスト
ユーザー GOTKAKO
提出日時 2025-12-15 01:41:55
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
WA  
実行時間 -
コード長 4,690 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,289 ms
コンパイル使用メモリ 209,556 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-15 01:42:00
合計ジャッジ時間 4,138 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 40 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

    vector<string> OK = {
        "8",
        "13",
        "267",
        "1125",
        "0246",
        "0269",
        "01279",
        "11269",
        "12679",
        "001299",
        "0112799",
        "00122457",
        "01224567",
        "000122459"
    };

    vector<int> C(10);
    string s; cin >> s;
    string answer = "";
    while(answer.size() <= s.size()) answer += '8';
    auto dfs = [&](auto dfs,int pos,int digit) -> void {
        if(digit > answer.size()) return;
        if(digit > s.size()){
            string now = "";
            auto memo = C;
            for(int i=1; i<=9; i++) if(C.at(i)){now += '0'+i,C.at(i)--; break;}
            for(int i=0; i<=9; i++) while(C.at(i)--) now += '0'+i;
            answer = min(answer,now);
            C = memo;
            return;
        }
        if(digit == s.size()){
            string now = "";
            auto memo = C;
            auto dfs2 = [&](auto dfs2,int d) -> bool {
                if(d == s.size()) return true;
                int c = s.at(d)-'0';
                if(C.at(c)){
                    C.at(c)--,now += s.at(d);
                    if(dfs2(dfs2,d+1)) return true;
                    C.at(c)++,now.pop_back();
                }
                for(int i=s.at(d)-'0'+1; i<=9; i++) if(C.at(i)){
                    C.at(i)--,now += '0'+i;
                    for(int k=0; k<=9; k++) while(C.at(k)--) now += '0'+k;
                    return true;
                }
                return false;
            };
            if(dfs2(dfs2,0)) answer = min(answer,now),C = memo;
            else C = memo,C.at(8)++,dfs(dfs,pos-1,digit+1);
            return;
        }
        if(pos < 0) return;
        dfs(dfs,pos-1,digit);
        if(digit+OK.at(pos).size() > answer.size()) return;
        auto memo = C;
        for(int i=digit+OK.at(pos).size(); i<=answer.size(); i+=OK.at(pos).size()){
            for(auto c : OK.at(pos)) C.at(c-'0')++;
            dfs(dfs,pos-1,i);
        }
        C = memo;
    };
    dfs(dfs,OK.size()-1,0);
    cout << answer << endl;
    return 0;
{
    vector<vector<int>> V = {
        {4,5,6},
        {},
        {0,2,3,4,6},
        {0,2,3,5,6},
        {1,2,3,5},
        {0,1,3,5,6},
        {1,3,4,5,6},
        {0,2,5},
        {},
        {0,1,2,3,5}
    };

    vector<int> C(10),ap(7);
    auto f = [&]() -> bool {
        for(int nine=0; nine<=C.at(9); nine++){
            vector<int> memo = ap;
            ap.at(6) += nine;
            int c = ap.at(6);
        
            int more = c-ap.at(3);
            bool ok = true;
            if(more >= 0 && more <= C.at(0)){
                ap.at(3) += more;
                ap.at(0) += C.at(0)-more,ap.at(1) += C.at(0)-more,ap.at(2) += C.at(0)-more;
                more = c-ap.at(5);
                if(more >= 0 && more <= C.at(1)){
                    ap.at(2) += more,ap.at(5) += more;
                    ap.at(1) += C.at(1)-more,ap.at(4) += C.at(1)-more;
                    more = c-ap.at(0);
                    if(more >= 0 && more <= C.at(6)){
                        ap.at(0) += more;
                        more = c-ap.at(1);
                        if(more >= 0 && more <= C.at(7)){
                            ap.at(1) += more;
                        }
                        else ok = false;
                    }
                    else ok = false;
                }
                else ok = false;
            }
            else ok = false;

            if(ok) for(int i=0; i<6; i++) if(ap.at(i) != ap.at(i+1)){ok = false; break;}
            ap = memo;
            if(ok) return true;
        }
        return false; 
    };
    for(auto c : s){
        C.at(c-'0')++;
        for(auto v : V.at(c-'0')) ap.at(v)++;
    }
    int n = s.size();
    set<string> S;
    int v = stoi(s);
    while(v <= 1000000000){
        if(v%1'0000'0000 == 0) cout << v << "!" << endl;
        v++;

        if(f()){
            auto t = s;
            sort(t.begin(),t.end());
            if(!S.count(t)) cout << s << "\n",S.insert(t);

        }
        for(int i=n-1; ; i--){
            if(i < 0){
                s = '1'+s,n++;
                for(auto v : V.at(1)) ap.at(v)++;
                C.at(1)++; break;
            }
            char &c = s.at(i);
            for(auto v : V.at(c-'0')) ap.at(v)--;
            C.at(c-'0')--;
            if(c == '9') c = '0';
            else c++;
            for(auto v : V.at(c-'0')) ap.at(v)++;
            C.at(c-'0')++;
            if(c == '0') continue;
            break;
        }
    }
    cout << s << endl;
}
}
0