結果

問題 No.3277 Forever Monotonic Number
ユーザー nonon
提出日時 2025-09-19 22:04:56
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 226 ms / 4,000 ms
コード長 1,429 bytes
コンパイル時間 3,289 ms
コンパイル使用メモリ 281,848 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-19 22:05:15
合計ジャッジ時間 5,353 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;

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

#include <atcoder/modint>
using mint = atcoder::modint998244353;

bool monotone(ll N) {
    string S = to_string(N);
    for (int i = 1; i < ssize(S); i++) {
        if (S[i] < S[i - 1]) return false;
    }
    return true;
}

ll D(ll N) {
    ll R = 0;
    while (N > 0) {
        R += N % 10;
        N /= 10;
    }
    return R;
}

bool f(ll N) {
    if (!monotone(N)) return false;
    return N < 10 ? true : f(D(N));
}

ll g(ll N) {
    auto S = to_string(N);
    char X = '0';
    bool fn = false;
    for (char &c : S) {
        if (fn) {
            c = X;
            continue;
        }
        if (c < X) {
            c = X;
            fn = true;
        } else {
            X = c;
        }
    }
    return stoll(S);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        ll N;
        cin >> N;
        ll M = ++N;
        while (!f(M)) {
            if (!monotone(M)) M = g(M);
            else M++;
        }
        mint ans = (mint(10).pow(N) - 1) / 9;
        M -= N;
        ll K = M / 8;
        M %= 8;
        ans += 8 * ((mint(10).pow(K) - 1) / 9);
        ans += M * mint(10).pow(K);
        cout << ans.val() << '\n';
    }
}
0