QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#127775#6744. SquareOrmlis#AC ✓138ms3444kbC++203.0kb2023-07-20 03:57:452023-07-20 03:57:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-20 03:57:49]
  • 评测
  • 测评结果:AC
  • 用时:138ms
  • 内存:3444kb
  • [2023-07-20 03:57:45]
  • 提交

answer

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;

int Bit(int mask, int b) { return (mask >> b) & 1; }

template<class T>
bool ckmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool ckmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return true;
    }
    return false;
}

const int INFi = 2e9;
const ll INF = 1e18;
const int LG = 18;

ll y;

// val * t + t * (t - 1) / 2 >= y - x
// 1/2 * t^2 + (val - 1/2) * t - (y - x) >= 0
// D = (val - 1/2)^2 + 2 * (y - x)
// t = (1/2 - val) + sqrt(D)

ll Add(ll x) {
    ll sx = sqrtl(2ll * x);
    while (sx * sx < 2 * x) sx++;
    while (sx * sx > 2 * x) sx--;
    if (sx * sx == 2 * x) return x + (sx + 1);
    if ((2 * sx + 1) * (2 * sx + 1) <= 8 * x) {
        return x + sx + 2;
    } else {
        return x + sx + 1;
    }
}

ll Go(ll x, ll cnt, ll val = -1) {
    if (val == -1) val = Add(x) - x;
    return x + val * cnt + cnt * (cnt - 1) / 2;
}

ll calc(ll x, ll val = -228, ll t = -228) {
    if (x >= y) return x - y;
    if (val == -228) val = Add(x) - x;
    if (t == -228) {
        t = (ld) ((ld) 0.5 - val) + sqrtl((val - (ld) 0.5) * (val - (ld) 0.5) + (ld) 2 * (y - x));
        while (Go(x, t, val) > y) t--;
        while (Go(x, t, val) < y) t++;
    }
//    cout << x << ' ' << val << ' ' << t << '\n';
    ll nu = 0;
    for(ll R = (1ll << 62ll); R >= 1; R /= 2) {
        if (R >= x) continue;
        ll x2 = x - R;
        ll val2 = Add(x2) - x2;
        if (Go(x2, t, val2) >= y) {
            x = x2;
            val = val2;
            nu += R;
        }
    }
//    cout << nu << '\n';
    assert(nu >= 0);
    ll g = Go(x, t, val);
    if (g == y) return t + nu;
    assert(g >= y);
    ll needt = g - y;
    if (needt <= 1) return nu + t + needt;
    if (needt < t) return nu + calc(Go(x, t - needt), val + (t - needt), needt) + (t - needt);
    return nu + calc(x + val, val + 1, t - 1) + 1;
}

void solve() {
    ll x; cin >> x;
    cin >> y;
    cout << calc(x) << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;
    int t = 1;
    cin >> t;
    rep(i, t) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3444kb

input:

2
5 1
1 5

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: 0
Accepted
time: 138ms
memory: 3420kb

input:

100000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 ...

output:

0
2
1
4
3
2
6
5
4
3
8
7
6
5
4
10
9
8
7
6
5
12
11
10
9
8
7
6
14
13
12
11
10
9
8
7
16
15
14
13
12
11
10
9
8
18
17
16
15
14
13
12
11
10
9
20
19
18
17
16
15
14
13
12
11
10
22
21
20
19
18
17
16
15
14
13
12
11
24
23
22
21
20
19
18
17
16
15
14
13
12
26
25
24
23
22
21
20
19
18
1
0
2
2
1
3
4
3
2
4
6
5
4
3
5
...

result:

ok 100000 numbers