結果

問題 No.3658 Darumaka Number 2
コンテスト
ユーザー aqua
提出日時 2026-08-30 14:16:21
言語 C++23
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,138 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,046 ms
コンパイル使用メモリ 332,620 KB
実行使用メモリ 8,440 KB
最終ジャッジ日時 2026-08-30 14:16:26
合計ジャッジ時間 4,323 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;

bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }
bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }

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

    string N; cin >> N;
    string ans;
    // f: strictly less than N
    auto solve = [&](auto self, int i, bool f) -> bool {
        if (i == N.size()) return true;
        if (f) {
            ans += '5';
            self(self, i + 1, true);
            return true;
        }
        if (N[i] > '5') {
            ans += '5';
            if (self(self, i + 1, true)) return true;
        } else if (N[i] == '5') {
            ans += '5';
            if (self(self, i + 1, f)) return true;
            ans.back() = '4';
            if (self(self, i + 1, true)) return true;
        } else if (N[i] == '4') {
            ans += '4';
            if (self(self, i + 1, f)) return true;
        }
        return false;
    };
    if (N[0] < '4') {
        ans += '5';
        solve(solve, 2, true);
    } else {
        solve(solve, 0, false);
    }
    cout << ans << '\n';
}
0