結果

問題 No.2287 ++ -- *=a /=a
コンテスト
ユーザー risujiroh
提出日時 2023-04-29 00:20:29
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,580 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,128 ms
コンパイル使用メモリ 332,364 KB
最終ジャッジ日時 2026-05-12 16:26:17
合計ジャッジ時間 2,735 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
In file included from main.cpp:145:
main.cpp: In function 'void std::ranges::views::solve()':
main.cpp:59:8: error: no matching function for call to 'print(int64_t&)'
   59 |   print(ans);
      |   ~~~~~^~~~~
main.cpp:59:8: note: there are 3 candidates
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/istream:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/sstream:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/complex:50,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:141,
                 from main.cpp:64:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ostream:259:5: note: candidate 1: 'template<class ... _Args> void std::print(ostream&, format_string<_Args ...>, _Args&& ...)'
  259 |     print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
      |     ^~~~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ostream:259:5: note: candidate expects at least 2 arguments, 1 provided
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:231:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/print:106:5: note: candidate 2: 'template<class ... _Args> void std::print(FILE*, format_string<_Args ...>, _Args&& ...)'
  106 |     print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
      |     ^~~~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/print:106:5: note: candidate expects at least 2 arguments, 1 provided
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/print:117:5: note: candidate 3: 'void std::print(format_string<_Args ...>, _Args&& ...) [with _Args = {}; format_string<_Args ...> = basic_format_string<char>]'
  117 |     print(format_string<_Args...> __fmt, _Args&&... __args)
      |     ^~~~~
/home/linuxbrew/.linuxbrew

ソースコード

diff #
raw source code

#if __INCLUDE_LEVEL__

void solve() {
  auto [x, y, a] = input<int64_t, 3>();
  auto go = [&](int64_t n) {
    vector<int64_t> ret;
    while (n) {
      ret.push_back(n % a);
      n /= a;
    }
    return ret;
  };
  auto vx = go(x);
  auto vy = go(y);
  show(vx);
  show(vy);
  vector<array<int64_t, 2>> f(size(vy) + 1);
  f[0][1] = 1;
  for (i : iota(1, ssize(f))) {
    f[i][0] = 1 + min(vy[i - 1] + f[i - 1][0], vy[i - 1] + 1 + f[i - 1][1]);
    f[i][1] =
        1 + min(a - vy[i - 1] + f[i - 1][0], a - vy[i - 1] - 1 + f[i - 1][1]);
  }
  auto eval = [&](auto&& v) {
    int64_t ret = 0;
    for (e : v | reverse) {
      ret *= a;
      ret += e;
    }
    return ret;
  };
  show(f);
  int64_t ans = abs(x - y);
  vector<int64_t> memo(ssize(vx) + 1);
  for (k : iota(0, ssize(memo))) {
    memo[k] = eval(vx | drop(k));
  }
  for (i : iota(0, ssize(vy) + 1)) {
    auto vy2 = ALL(vector, vy | drop(i));
    for (ty = eval(vy2); int k : iota(0, ssize(vx) + 1)) {
      ans = min(ans, k + abs(memo[k] - ty) + f[i][0]);
    }
    {
      int j = 0;
      while (j < ssize(vy2) && vy2[j] + 1 == a) {
        vy2[j] = 0;
        ++j;
      }
      if (j < ssize(vy2)) {
        ++vy2[j];
      } else {
        vy2.push_back(1);
      }
    }
    for (ty = eval(vy2); int k : iota(0, ssize(vx) + 1)) {
      ans = min(ans, k + abs(memo[k] - ty) + f[i][1]);
    }
  }
  print(ans);
}

#else  // __INCLUDE_LEVEL__

#include <bits/stdc++.h>

namespace std {

template <class T>
auto operator>>(istream& is, T&& t) -> decltype(tuple_cat(t), is) {
  return apply([&is](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}

template <class T>
auto operator<<(ostream& os, T&& t) -> decltype(tuple_cat(t), os) {
  auto f = [&os](auto&... xs) -> ostream& {
    [[maybe_unused]] auto sep = "";
    ((os << exchange(sep, " ") << xs), ...);
    return os;
  };
  return apply(f, t);
}

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto&& e : v) {
    is >> e;
  }
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  static constexpr auto SEP = is_convertible_v<int, T> ? " " : "\n";
  for (auto sep = ""; const auto& e : v) {
    os << exchange(sep, SEP) << e;
  }
  return os;
}

}  // namespace std

template <class... Ts, class... Args>
auto input(Args&&... args) {
  if constexpr (sizeof...(Ts) == 0) {
    static_assert(sizeof...(Args) == 0);
    return input<int>();
  } else if constexpr (1 < sizeof...(Ts)) {
    static_assert(sizeof...(Args) == 0);
    return input<std::tuple<Ts...>>();
  } else {
    using T = std::tuple_element_t<0, std::tuple<Ts...>>;
    if constexpr (sizeof...(args)) {
      T x(std::forward<Args>(args)...);
      std::cin >> x;
      return x;
    } else {
      T x;
      std::cin >> x;
      return x;
    }
  }
}

template <class T, int N>
std::array<T, N> input() {
  return input<std::array<T, N>>();
}

template <class... Ts>
void print(Ts&&... xs) {
  std::cout << std::tie(xs...) << '\n';
}

using namespace std;

#define show(...) static_cast<void>(0)
#define for(...) for ([[maybe_unused]] auto&& __VA_ARGS__)
#define lambda(...) [&](auto&&... args) { return __VA_ARGS__; }
#define $1 get<0>(tie(args...))
#define $2 get<1>(tie(args...))
#define ALL(f, r, ...) lambda(f(begin($1), end($1), ##__VA_ARGS__))(r)

namespace std::ranges::views {

#include __BASE_FILE__

}  // namespace std::ranges::views

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  for (_ : views::iota(0, input())) {
    views::solve();
  }
}

#endif  // __INCLUDE_LEVEL__
0