結果

問題 No.2287 ++ -- *=a /=a
ユーザー みここみここ
提出日時 2023-04-22 18:07:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 1,593 bytes
コンパイル時間 680 ms
コンパイル使用メモリ 79,740 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 22:24:51
合計ジャッジ時間 2,867 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 30 ms
5,376 KB
testcase_07 AC 30 ms
5,376 KB
testcase_08 AC 31 ms
5,376 KB
testcase_09 AC 24 ms
5,376 KB
testcase_10 AC 23 ms
5,376 KB
testcase_11 AC 24 ms
5,376 KB
testcase_12 AC 22 ms
5,376 KB
testcase_13 AC 22 ms
5,376 KB
testcase_14 AC 23 ms
5,376 KB
testcase_15 AC 22 ms
5,376 KB
testcase_16 AC 22 ms
5,376 KB
testcase_17 AC 22 ms
5,376 KB
testcase_18 AC 39 ms
5,376 KB
testcase_19 AC 35 ms
5,376 KB
testcase_20 AC 33 ms
5,376 KB
testcase_21 AC 32 ms
5,376 KB
testcase_22 AC 32 ms
5,376 KB
testcase_23 AC 32 ms
5,376 KB
testcase_24 AC 31 ms
5,376 KB
testcase_25 AC 31 ms
5,376 KB
testcase_26 AC 32 ms
5,376 KB
testcase_27 AC 54 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

const ll INF = 2000000000000000000;

struct S
{
    ll prefix;
    ll prefix_dist;
    ll prefixpp_dist;
};

void solve()
{
    int t;
    cin >> t;
    while(t--)
    {
        ll x, y, a;
        vector<S> dp;
        cin >> x >> y >> a;
        ll c = 0;
        dp.push_back(S{y, INF, INF});
        while(true)
        {
            dp.back().prefix_dist = min(dp.back().prefix_dist, c + abs(y - x));
            dp.back().prefixpp_dist = min(dp.back().prefixpp_dist, c + abs(y + 1 - x));
            if(y == 0 && x == 0)
            {
                break;
            }
            if(y >= x)
            {
                y /= a;
                dp.push_back(S{y, INF, INF});
            }
            else
            {
                x /= a;
                c++;
            }
        }
        int n = dp.size();
        for(int i = n - 2; i >= 0; i--)
        {
            dp[i].prefix_dist =
                min({dp[i].prefix_dist,
                     dp[i + 1].prefix_dist + dp[i].prefix - dp[i + 1].prefix * a + 1,
                     dp[i + 1].prefixpp_dist + (dp[i + 1].prefix + 1) * a - dp[i].prefix + 1});
            dp[i].prefixpp_dist =
                min({dp[i].prefixpp_dist,
                     dp[i + 1].prefix_dist + (dp[i].prefix + 1) - dp[i + 1].prefix * a + 1,
                     dp[i + 1].prefixpp_dist + (dp[i + 1].prefix + 1) * a - (dp[i].prefix + 1) + 1});
        }
        cout << dp[0].prefix_dist << endl;
    }
}

int main()
{
    solve();
}
0