結果

問題 No.2287 ++ -- *=a /=a
ユーザー milanis48663220milanis48663220
提出日時 2023-04-28 23:43:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 807 ms / 2,000 ms
コード長 3,605 bytes
コンパイル時間 1,367 ms
コンパイル使用メモリ 131,236 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 00:45:56
合計ジャッジ時間 4,143 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 36 ms
5,376 KB
testcase_07 AC 35 ms
5,376 KB
testcase_08 AC 37 ms
5,376 KB
testcase_09 AC 25 ms
5,376 KB
testcase_10 AC 21 ms
5,376 KB
testcase_11 AC 20 ms
5,376 KB
testcase_12 AC 19 ms
5,376 KB
testcase_13 AC 19 ms
5,376 KB
testcase_14 AC 19 ms
5,376 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 AC 18 ms
5,376 KB
testcase_18 AC 227 ms
5,376 KB
testcase_19 AC 104 ms
5,376 KB
testcase_20 AC 75 ms
5,376 KB
testcase_21 AC 61 ms
5,376 KB
testcase_22 AC 53 ms
5,376 KB
testcase_23 AC 49 ms
5,376 KB
testcase_24 AC 45 ms
5,376 KB
testcase_25 AC 42 ms
5,376 KB
testcase_26 AC 40 ms
5,376 KB
testcase_27 AC 807 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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));
        if(xx == 0){
            break;
        }
        cnt_devide++;
        xx /= a;
    }
    return ans;
}

void test(int a){
    for(int x = 1; x <= 40; x++){
        for(int y = 1; y <= 40; 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(24, 1, 5) << 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;
    }
}
0