結果

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vmi = vector<mint>;
using vvmi = vector<vmi>;
using vvvmi = vector<vvmi>;
#define all(a) (a).begin(), (a).end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
ll mono(ll s){
    string t = to_string(s);
    int len = t.length();
    rep(i, len-1){
        int a = t[i] - '0';
        int b = t[i+1] - '0';
        if(a > b){
            vector<char> v(len);
            rep(j, i)v[j] = t[j];
            rep2(j, i, len)v[j] = t[i];
            string res(v.begin(), v.end());
            return stoll(res);

        }
    }
    return s;
}

ll keta(ll s){
    string t = to_string(s);
    int len = t.length();
    ll res = 0;
    rep(i, len)res += t[i] - '0';
    return res;
}

int main(){
    int t; cin >> t;
    while(t--){
        ll n; cin >> n;
        if(n <= 8){
            string s; rep(i, n+1)s.append("1");
            ll u = stoll(s);
            mint ans = mint(u);
            cout << ans.val() << endl;
            continue;
        }

        ll base = n+1;
        while(true){
            ll c = mono(base);
            ll w = c;
            bool f = true;
            while(w > 9){
                if(mono(keta(w)) != keta(w)){
                    f = false;
                    break;
                }
                w = keta(w);
            }
            if(f){
                base = c;
                break;
            }
            if(c == base){
                base++;
            }else{
                base = c;
            }
        }
        ll amari = base - n - 1;
        ll cnine = amari/8;
        ll nokori = (amari%8)+1;

        mint ans = (mint(10).pow(cnine) - mint(1));
        mint bb = mint(10).pow(cnine);
        

        ans += bb*mint(nokori);
        ll cone = n - cnine;
        mint ss = (mint(10).pow(cone) - mint(1))/mint(9);
        ans += ss*mint(10).pow(cnine+1);
        cout << ans.val() << endl;
    }

    return 0;
}
0