結果

問題 No.2287 ++ -- *=a /=a
ユーザー jianglyjiangly
提出日時 2023-04-28 22:18:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 650 ms / 2,000 ms
コード長 1,038 bytes
コンパイル時間 1,966 ms
コンパイル使用メモリ 198,004 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 07:13:53
合計ジャッジ時間 6,211 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 101 ms
4,376 KB
testcase_07 AC 102 ms
4,380 KB
testcase_08 AC 102 ms
4,380 KB
testcase_09 AC 81 ms
4,380 KB
testcase_10 AC 62 ms
4,380 KB
testcase_11 AC 56 ms
4,380 KB
testcase_12 AC 50 ms
4,380 KB
testcase_13 AC 49 ms
4,380 KB
testcase_14 AC 48 ms
4,376 KB
testcase_15 AC 46 ms
4,376 KB
testcase_16 AC 45 ms
4,380 KB
testcase_17 AC 42 ms
4,380 KB
testcase_18 AC 335 ms
4,380 KB
testcase_19 AC 221 ms
4,380 KB
testcase_20 AC 180 ms
4,376 KB
testcase_21 AC 160 ms
4,376 KB
testcase_22 AC 146 ms
4,376 KB
testcase_23 AC 139 ms
4,376 KB
testcase_24 AC 129 ms
4,380 KB
testcase_25 AC 124 ms
4,380 KB
testcase_26 AC 120 ms
4,376 KB
testcase_27 AC 650 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;

void solve() {
    i64 x, y, a;
    std::cin >> x >> y >> a;
    
    i64 ans = 1E18;
    
    i64 dp[61][2];
    dp[0][0] = 0;
    dp[0][1] = 1;
    
    i64 v = y;
    for (int i = 1; i <= 60; i++) {
        i64 t = v % a;
        dp[i][0] = std::min(dp[i - 1][0] + t, dp[i - 1][1] + t + 1);
        dp[i][1] = std::min(dp[i - 1][0] + a - t, dp[i - 1][1] + a - t - 1);
        v /= a;
    }
    
    i64 cur = 0;
    while (true) {
        
        i64 v = y;
        for (int i = 0; i <= 60; i++) {
            ans = std::min(ans, cur + dp[i][0] + std::abs(x - v) + i);
            ans = std::min(ans, cur + dp[i][1] + std::abs(x - v - 1) + i);
            v /= a;
        }
        
        if (!x) {
            break;
        }
        cur++;
        x /= a;
    }
    
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}
0