結果

問題 No.3157 Nabeatsu
ユーザー twins_fuyu
提出日時 2025-05-23 19:56:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 994 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 199,168 KB
実行使用メモリ 13,036 KB
最終ジャッジ日時 2025-05-23 19:57:00
合計ジャッジ時間 5,179 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;

string s;

int main(){
    cin >> s;
    vector<int> num;
    ll sum = 0;
    bool flag = false;
    for (char c: s){
        int n = c - '0';
        if (flag){
            num.push_back(9);
            sum += 9;
            sum %= 3;
        }
        else if (n == 3){
            num.push_back(2);
            flag = true;
            sum += 2;
        }
        else{
            num.push_back(n);
            sum += n;
        }
    }

    if (sum % 3 == 0){
        auto tmp = num;
        ll idx = num.size() - 1;
        num[idx] --;
        if (num[idx] == 3){
            num[idx] = 2;
        }
        while(num[idx]== -1){
            num[idx] = 9;
            idx --;
            num[idx] --;
            if (num[idx] == 3){
                num[idx] = 2;
            }
        }
    }
    string ans;
    for (int i = 0; i < num.size(); i++){
        ans += to_string(num[i]);
    }
    cout << ans << endl;
}
0