結果

問題 No.2287 ++ -- *=a /=a
ユーザー みここみここ
提出日時 2023-02-11 02:11:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,568 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 78,700 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 22:25:05
合計ジャッジ時間 3,189 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 AC 23 ms
5,376 KB
testcase_10 AC 23 ms
5,376 KB
testcase_11 AC 23 ms
5,376 KB
testcase_12 AC 22 ms
5,376 KB
testcase_13 AC 22 ms
5,376 KB
testcase_14 AC 24 ms
5,376 KB
testcase_15 AC 23 ms
5,376 KB
testcase_16 AC 22 ms
5,376 KB
testcase_17 AC 23 ms
5,376 KB
testcase_18 RE -
testcase_19 WA -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 WA -
testcase_24 RE -
testcase_25 WA -
testcase_26 RE -
testcase_27 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int INF = 2000000000;

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

void solve()
{
    int t;
    cin >> t;
    while(t--)
    {
        int x, y, a;
        vector<S> dp;
        cin >> x >> y >> a;
        int 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