結果

問題 No.2287 ++ -- *=a /=a
ユーザー みここ
提出日時 2023-02-11 02:11:46
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,568 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 105,704 KB
最終ジャッジ日時 2025-02-10 13:57:19
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14 WA * 4 RE * 9
権限があれば一括ダウンロードができます

ソースコード

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