結果

問題 No.3157 Nabeatsu
コンテスト
ユーザー tnakao0123
提出日時 2025-05-24 16:38:20
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 40 ms / 2,000 ms
+ 220µs
コード長 988 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 184 ms
コンパイル使用メモリ 56,376 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-11 01:46:46
合計ジャッジ時間 2,871 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3157.cc:  No.3157 Nabeatsu - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_L = 1000000;

/* typedef */

/* global variables */

char s[MAX_L + 4];

/* subroutines */

void dec(int l, char s[]) {
  int co = 1;
  for (int i = l - 1; co && i >= 0; i--) {
    if (s[i] == '0') s[i] = '9';
    else s[i]--, co = 0;
  }
}

int dig3(int l, char s[]) {
  for (int i = 0; i < l; i++)
    if (s[i] == '3') return i;
  return -1;
}

bool multiple3(int l, char s[]) {
  int sum = 0;
  for (int i = 0; i < l; i++) sum += (s[i] - '0');
  return (sum % 3 == 0);
}

/* main */

int main() {
  scanf("%s", s);
  int l = strlen(s);

  for (;;) {
    //puts(s);

    int d3 = dig3(l, s);
    //printf(" d3=%d\n", d3);

    if (d3 >= 0) {
      s[d3] = '2';
      for (int i = d3 + 1; i < l; i++) s[i] = '9';
    }

    if (multiple3(l, s)) dec(l, s);
    else break;
  }
  
  puts(s);
  return 0;
}
0