結果

問題 No.2287 ++ -- *=a /=a
ユーザー KudeKude
提出日時 2023-04-28 23:40:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,785 bytes
コンパイル時間 2,307 ms
コンパイル使用メモリ 229,796 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-29 00:44:06
合計ジャッジ時間 7,471 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 315 ms
6,944 KB
testcase_07 AC 318 ms
6,944 KB
testcase_08 AC 322 ms
6,940 KB
testcase_09 AC 24 ms
6,944 KB
testcase_10 AC 13 ms
6,944 KB
testcase_11 AC 11 ms
6,940 KB
testcase_12 AC 9 ms
6,940 KB
testcase_13 AC 9 ms
6,944 KB
testcase_14 AC 9 ms
6,944 KB
testcase_15 AC 8 ms
6,944 KB
testcase_16 AC 7 ms
6,944 KB
testcase_17 AC 8 ms
6,940 KB
testcase_18 TLE -
testcase_19 AC 811 ms
6,940 KB
testcase_20 AC 428 ms
6,944 KB
testcase_21 AC 289 ms
6,944 KB
testcase_22 AC 222 ms
6,948 KB
testcase_23 AC 180 ms
6,944 KB
testcase_24 AC 155 ms
6,940 KB
testcase_25 AC 127 ms
6,944 KB
testcase_26 AC 110 ms
6,944 KB
testcase_27 TLE -
権限があれば一括ダウンロードができます

ソースコード

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--) {
          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