結果

問題 No.2287 ++ -- *=a /=a
ユーザー jianglyjiangly
提出日時 2023-04-28 22:18:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 654 ms / 2,000 ms
コード長 1,038 bytes
コンパイル時間 2,058 ms
コンパイル使用メモリ 199,800 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 23:50:32
合計ジャッジ時間 6,052 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 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 101 ms
5,376 KB
testcase_07 AC 101 ms
5,376 KB
testcase_08 AC 102 ms
5,376 KB
testcase_09 AC 82 ms
5,376 KB
testcase_10 AC 62 ms
5,376 KB
testcase_11 AC 56 ms
5,376 KB
testcase_12 AC 51 ms
5,376 KB
testcase_13 AC 50 ms
5,376 KB
testcase_14 AC 48 ms
5,376 KB
testcase_15 AC 46 ms
5,376 KB
testcase_16 AC 45 ms
5,376 KB
testcase_17 AC 44 ms
5,376 KB
testcase_18 AC 337 ms
5,376 KB
testcase_19 AC 221 ms
5,376 KB
testcase_20 AC 182 ms
5,376 KB
testcase_21 AC 160 ms
5,376 KB
testcase_22 AC 147 ms
5,376 KB
testcase_23 AC 139 ms
5,376 KB
testcase_24 AC 131 ms
5,376 KB
testcase_25 AC 125 ms
5,376 KB
testcase_26 AC 121 ms
5,376 KB
testcase_27 AC 654 ms
5,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