結果
| 問題 |
No.2287 ++ -- *=a /=a
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-04-22 18:07:04 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 47 ms / 2,000 ms |
| コード長 | 1,593 bytes |
| コンパイル時間 | 974 ms |
| コンパイル使用メモリ | 76,840 KB |
| 最終ジャッジ日時 | 2025-02-12 13:06:48 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 27 |
ソースコード
#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();
}