結果

問題 No.3157 Nabeatsu
ユーザー k3g
提出日時 2025-05-25 22:13:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,972 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 197,324 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-25 22:13:14
合計ジャッジ時間 4,551 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define rep1(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep(...) _overload3(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(x) (x).begin(),(x).end()
using ll=long long;
using P=pair<ll,ll>;
const ll LINF = 8e18; 
template <typename T>bool chmax(T &a, const T& b) {if (a < b) {a = b;return true;}return false;}
template <typename T>bool chmin(T &a, const T& b) {if (a > b) {a = b;return true;}return false;}
template <typename T>ll ipow(T n, int a) {ll p = 1; rep(i,a)p*=n;return p;}
template <typename T>void vout(vector<T> &v,int str = 0, int sum=0){if(sum){cout<<v.size()<<"\n";}for(int i=0;i<(int)(v.size());i++){if(i) cout<<(str ? "\n" : " ");cout<<v[i];}cout<<"\n";}


int main (){
    string s;
    cin >> s;
    auto chk = [&](string t)->bool{
        ll cnt = 0;
        rep(i,t.size()){
            ll num =  t[i] -'0';
            if(num == 3)return true;
            cnt += num;
        }
        if(cnt%3 == 0)return true;

        return false;
    };

    //文字列で1引く関数
    auto subone =[&](string t)->string{
        if(t == "0") return "err"; //0だけは別に処理しておく
        rrep(i, t.size()){
            if(t[i] != '0'){
                t[i]--;
                break;
            }
            //繰り下がり
            t[i] = '9';
        }
        //先頭のゼロを削除用
        ll ind = 0;
        //while(ind + 1 < t.size() && t[ind] == '0') ind++;
        return t.substr(ind);
    };

    while(chk(s)){
        auto it = find(all(s),'3');
        if(it == s.end())s = subone(s);
        else {
            //3を含む場合は一気に減らす
            *it = '2';
            it++;
            while(it != s.end()) *it = '9',it++;
        }
    }

    cout << s << endl;

    return 0;
}
0