結果

問題 No.2287 ++ -- *=a /=a
ユーザー KudeKude
提出日時 2023-04-28 23:42:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 476 ms / 2,000 ms
コード長 1,836 bytes
コンパイル時間 2,524 ms
コンパイル使用メモリ 230,864 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 00:44:58
合計ジャッジ時間 5,384 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 54 ms
5,376 KB
testcase_07 AC 54 ms
5,376 KB
testcase_08 AC 55 ms
5,376 KB
testcase_09 AC 16 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 10 ms
5,376 KB
testcase_13 AC 9 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 8 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 476 ms
5,376 KB
testcase_19 AC 191 ms
5,376 KB
testcase_20 AC 126 ms
5,376 KB
testcase_21 AC 95 ms
5,376 KB
testcase_22 AC 81 ms
5,376 KB
testcase_23 AC 71 ms
5,376 KB
testcase_24 AC 63 ms
5,376 KB
testcase_25 AC 59 ms
5,376 KB
testcase_26 AC 55 ms
5,376 KB
testcase_27 AC 471 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while(tt--) {
    ll x, y, a;
    cin >> x >> y >> a;
    auto digits = [](ll x, ll a) {
      VL d;
      while(x) d.emplace_back(x % a), x /= a;
      return d;
    };
    VL dx = digits(x, a);
    VL dy = digits(y, a);
    int sz = max(dx.size(), dy.size());
    dx.resize(sz);
    dy.resize(sz);
    VL vx(sz + 1), vy(sz + 1);
    rrep(i, sz) {
      vx[i] = a * vx[i + 1] + dx[i];
      vy[i] = a * vy[i + 1] + dy[i];
    }
    ll ans = 4e18;
    rep(to, sz + 1) {
      ll x0 = x;
      rep(_, to) x0 /= a;
      rep(at, sz + 1) {
        ll dp[2]{to + abs(vy[at] - x0), to + abs(vy[at] + 1 - x0)};
        for(int i = at - 1; i >= 0; i--) {
          if (dp[0] >= ans && dp[1] >= ans) break;
          ll ndp[2]{(ll)4e18, (ll)4e18};
          rep(x, 2) rep(y, 2) {
            ll p = (vy[i + 1] + x) * a;
            ll cost = 1 + abs(vy[i] + y - p);
            chmin(ndp[y], dp[x] + cost);
          }
          swap(dp, ndp);
        }
        chmin(ans, dp[0]);
      }
    }
    cout << ans << '\n';
  }
}
0