結果

問題 No.3277 Forever Monotonic Number
ユーザー 👑 loop0919
提出日時 2025-09-08 23:13:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 428 ms / 4,000 ms
コード長 2,084 bytes
コンパイル時間 3,543 ms
コンパイル使用メモリ 291,492 KB
実行使用メモリ 22,388 KB
最終ジャッジ日時 2025-09-10 08:39:22
合計ジャッジ時間 8,006 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
using mint = modint998244353;

vector<ll> fm_vec;

bool is_monotonic(ll n) {
    string str_n = to_string(n);

    for (int i = 0; i < str_n.size() - 1; i++) {
        if (str_n.at(i) > str_n.at(i + 1)) {
            return false;
        }
    }

    return true;
}

int digit_sum(ll n) {
    string str_n = to_string(n);
    int sum = 0;

    for (char c: str_n) {
        sum += int(c - '0');
    }

    return sum;
}

void init_forever_monotonic(int len) {
    vector<bool> is_fm(9 * len + 1, false);
    for (int i = 0; i <= 9 * len; i++) {
        if (i <= 9 || (is_monotonic(i) && is_fm[digit_sum(i)])) {
            is_fm[i] = true;
        }
    }

    vector<ll> pow_ten(len, 1);
    for (int i = 1; i < len; i++) {
        pow_ten[i] = pow_ten[i - 1] * 10;
    }

    vector mono_vec(len, vector(10, vector<ll>()));
    for (int i = 0; i <= 9; i++) {
        mono_vec[0][i].push_back(i);
    }

    for (int i = 0; i <= 9; i++) {
        fm_vec.push_back(i);
    }

    for (ll d = 1; d < len; d++) {
        for (int i = 1; i <= 9; i++) {
            for (int j = i; j <= 9; j++) {
                for (ll mono: mono_vec[d - 1][j]) {
                    ll next_mono = i * pow_ten[d] + mono;
                    mono_vec[d][i].push_back(next_mono);

                    if (is_fm[digit_sum(next_mono)]) {
                        fm_vec.push_back(next_mono);
                    }
                }
            }
        }
    }
}

void solve() {
    ll N;
    cin >> N;

    ll digit = *lower_bound(fm_vec.begin(), fm_vec.end(), N + 1);

    ll div = (digit - (N + 1)) / 8;
    ll rem = (digit - (N + 1)) % 8;

    mint ans = (mint(10).pow(N + 1) - 1) / 9;
    ans += (mint(10).pow(div + 1) - 1) * rem / 9;
    ans += (mint(10).pow(div) - 1) * (8 - rem) / 9;

    cout << ans.val() << "\n";
}

int main() {
    // 10^15 未満の forever-monotonic な整数を列挙
    init_forever_monotonic(15);

    int T;
    cin >> T;
    while (T--) solve();
}
0