QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#231687#6510. Best Carry Player 3ucup-team1198#TL 0ms3400kbC++201.7kb2023-10-29 15:35:372023-10-29 15:35:37

Judging History

你现在查看的是最新测评结果

  • [2023-10-29 15:35:37]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3400kb
  • [2023-10-29 15:35:37]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()

ll get(ll x, ll y, ll k, ll b) {
    ll tail = (1ll << b) - 1;
    ll xt = x & tail;
    ll yt = y & tail;

    ll xs = (x >> b);
    ll ys = (y >> b);

    ll res = 0;

    if (x == y) return 0;
    if ((xs == ys) || (x + 1 == y) || ((x ^ y) < k)) return 1;
    
    if (xs + 1 == ys) {
        if ((xs % 2 == 0) && (k != (1ll << b))) {
            res = 2;
        } else {
            res = 3;
            if (xt == tail) res--;
            if (yt == 0) res--;
        }
        return res;
    }

    if (k == (1ll << b)) {
        return 2 * (ys - xs - 1) + get((ys - 1) << b, y, k, b);
    }
    if (xs % 2 == 1) {
        return 2 + get((xs + 1) << b, y, k, b);
    } else {
        ll h = (ys - xs) / 2;
        return 3 * h + get(((xs + 2 * h) << b), y, k, b);
    }

    // if (xs % 2)
}

void solve() {
    ll x, y, k;
    cin >> x >> y >> k;
    if (x > y) swap(x, y);

    if (x == y) {
        cout << "0\n";
        return;
    }

    if (((x ^ y) <= k) || (y - x <= 1)) {
        cout << "1\n";
        return;
    }

    ++k;

    int b = 0;
    for (int i = 0; i < 60; ++i) {
        if ((k >> i) & 1) b = i;
    }

    ll res = min(get(x, y, k, b), get(x + 1, y, k, b) + 1);
    
    ll tail = (1ll << b) - 1;
    ll xt = x & tail;
    // ll yt = y & tail;

    if ((xt ^ ((1ll << (b + 1)) - 1)) < k) res = min(res, 2 + get((x ^ (xt ^ ((1ll << (b + 1)) - 1))) + 1, y, k, b));

    cout << res << '\n';
    return;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while(t--) solve();


    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3400kb

input:

8
4 5 0
5 8 3
9 2 6
15 28 5
97 47 8
164 275 38
114514 1919 810
0 1152921504606846975 1

output:

1
2
3
5
11
6
331
1152921504606846975

result:

ok 8 numbers

Test #2:

score: -100
Time Limit Exceeded

input:

100000
84 318 6
54 226 7
92 33 0
39 54 5
76 79 7
247 110 0
211 90 0
4 430 3
230 17 1
491 93 5
196 117 7
137 29 2
76 490 6
422 43 7
277 26 4
159 43 1
67 37 5
17 2 5
113 176 7
85 473 0
68 217 7
275 8 7
124 34 1
30 66 0
80 149 3
103 149 6
84 354 1
27 342 7
94 114 1
69 125 1
72 48 7
361 8 7
285 82 1
74 ...

output:


result: