結果
問題 | No.2287 ++ -- *=a /=a |
ユーザー | milanis48663220 |
提出日時 | 2023-04-28 23:39:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,554 bytes |
コンパイル時間 | 1,448 ms |
コンパイル使用メモリ | 131,744 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-17 22:29:20 |
合計ジャッジ時間 | 3,859 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 740 ms
6,816 KB |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <cmath> #include <numeric> #include <functional> #include <cassert> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template<typename T> vector<vector<T>> vec2d(int n, int m, T v){ return vector<vector<T>>(n, vector<T>(m, v)); } template<typename T> vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){ return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v))); } template<typename T> void print_vector(vector<T> v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } ll naive(ll x, ll y, ll a){ map<ll, ll> dist; map<ll, ll> pre; queue<ll> que; auto push = [&](ll v, ll d, ll prev_value){ if(dist.count(v)) return; dist[v] = d; pre[v] = prev_value; que.push(v); }; push(x, 0, -1); while(true){ ll v = que.front(); que.pop(); ll d = dist[v]; if(v == y) break; push(v+1, d+1, v); if(v > 0) push(v-1, d+1, v); push(v/a, d+1, v); push(v*a, d+1, v); } vector<ll> path; ll cur = y; while(cur != -1){ path.push_back(cur); cur = pre[cur]; } reverse(path.begin(), path.end()); // print_vector(path); return dist[y]; } const ll inf = 4e18; // 割り算以外を使って最小何回か ll sub_solve(ll x, ll y, ll a){ int cnt = 0; ll l = y, r = y; ll ans = abs(y-x); ll cl = 0, cr = 0; while(true){ chmin(ans, cl+abs(l-x)); chmin(ans, cr+abs(r-x)); if(l == 0){ break; } ll l0 = l/a; ll r0 = l0+1; ll l0a = l0*a; ll r0a = r0*a; ll cl0 = min(abs(l-l0a)+cl, abs(r-l0a)+cr)+1; ll cr0 = min(abs(l-r0a)+cl, abs(r-r0a)+cr)+1; cl = min(cl0, cr0+1); cr = min(cr0, cl0+1); l = l0; r = r0; // cout << l0a << ' ' << r0a << endl; // cout << l << ' ' << r << ':' << cl << ' ' << cr << endl; } // cout << x << "->" << y << ' ' << ans << endl; return ans; } ll solve(ll x, ll y, ll a){ ll cnt_devide = 0; ll ans = abs(y-x); ll xx = x; while(xx > 0){ chmin(ans, cnt_devide+sub_solve(xx, y, a)); cnt_devide++; xx /= a; } return ans; } void test(int a){ for(int x = 1; x <= 30; x++){ for(int y = 1; y <= 30; y++){ ll a0 = naive(x, y, a); ll a1 = solve(x, y, a); if(a0 == a1) continue; cout << x << "->" << y << ":" << a0 << ' ' << a1 << endl; // naive(x, y, a); } debug_value(x) } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; // cout << sub_solve(1, 24, 3) << endl; // int a; cin >> a; // test(a); int t; cin >> t; while(t--){ ll x, y, a; cin >> x >> y >> a; cout << solve(x, y, a) << endl; } }