結果
| 問題 | No.3157 Nabeatsu | 
| コンテスト | |
| ユーザー |  ゼリトキ | 
| 提出日時 | 2025-05-23 19:33:39 | 
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 7 ms / 2,000 ms | 
| コード長 | 1,195 bytes | 
| コンパイル時間 | 2,772 ms | 
| コンパイル使用メモリ | 276,396 KB | 
| 実行使用メモリ | 7,844 KB | 
| 最終ジャッジ日時 | 2025-05-23 19:34:00 | 
| 合計ジャッジ時間 | 4,751 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 45 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ll long long
const long long mod=998244353;
const long long hmod=46216567629137;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    string N;
    cin>>N;
    ll cnt=0;
    ll max_three=-1;
    rep(i,N.size()){
        cnt+=N[i]-'0';
        if(max_three==-1 && N[i]=='3') max_three=i;
    }
    if(max_three==-1 && cnt%3!=0){
        cout<<N<<"\n";
        return 0;
    }
    int siz=N.size();
    if(cnt%3==0 && max_three==-1){
        bool check=0;
        for(int i=siz-1;i>=0;i--){
            if(N[i]=='0'){
                N[i]='9';
            }
            else{
                N[i]--;
                if(N[i]=='3'){
                    check=1;
                    max_three=i;
                }
                break;
            }
        }
        if(check==0){
            cout<<N<<"\n";
            return 0;
        }
    }
    N[max_three]='2';
    cnt=0;
    for(int i=0;i<=max_three;i++) cnt+=N[i]-'0';
    for(int i=max_three+1;i<siz;i++){
        N[i]='9';
        cnt+=N[i]-'0';
    }
    if(cnt%3==0){
        N[siz-1]--;
    }
    cout<<N<<"\n";
}
            
            
            
        