DMOJ · ccc26s1

CCC 2026 S1 Baby Hop, Giant Hop

This C++ solution uses constant-time case analysis for CCC 2026 S1 Baby Hop, Giant Hop. Read the reasoning, inspect the code, or try your own test case below.

ccc26s1CCC 2026 S1MathConstant-time case analysisC++30 lines
Solution020of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Constant-time case analysis

Compare the target distance with the giant-hop length K. The quotient and remainder tell us the shortest route; the second-shortest route comes from the closest valid alternative using one extra unit or giant hop.

CCCArithmetic
Time
O(1)
Space
O(1)

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

S_1_Baby_Hop_Giant_Hop.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    using i128 = __int128;
    const int inf = 1e9;
    const ll INF = 1LL << 60;
    int main() {
        ios::sync_with_stdio(0); cin.tie(0);
        ll a, b, k, t; cin >> a >> b >> k >> t;
        if (t == 1) {
            if (b < a) swap(a, b);
            i128 s = (i128)(b - a) / k;
            ll ans1 = (b - a) % k + s;
            ll ans2 = (i128)(s + 1LL) * (i128)k - (b - a) + s + 1;
            cout << min(ans1, ans2) << '\n';
        } else {
            if (b < a) swap(a, b);
            i128 s = (i128)(b - a) / k;
            ll ans1 = (b - a) % k + s;
            ll ans2 = (i128)(s + 1LL) * (i128)k - (b - a) + s + 1;
            ll ans3 = (s >= 1 ? (i128)(b - a) - (i128)(s - 1LL) * (i128)k + (i128)(s - 1LL) : INF);
            ll best = min({b - a, ans1, ans2, ans3});
            ll ans = best + 2;
            for (ll x : {ans1, ans2, ans3, b - a}) {
                if (x > best) ans = min(ans, x);
            }
            cout << ans << '\n';
        }
        return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.