QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#483387 | #6744. Square | ucup-team052# | AC ✓ | 71ms | 3920kb | C++14 | 2.3kb | 2024-07-18 16:34:56 | 2024-07-18 16:34:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
ll s, t;
ll sum(ll l, ll r) {
return 1ll * (l + r) * (r - l + 1) / 2;
}
ll getdt(ll x) {
ll dt = floor(sqrtl(2 * x) + 1.5);
++dt;
if (2 * x - dt * dt + 3 * dt >= 2.25) return dt;
return dt - 1;
}
ll getnxt(ll x) {
return x + getdt(x);
}
int main() {
scanf("%lld", &T);
while (T--) {
scanf("%lld%lld", &s, &t);
ll ans = 0;
while (t > s) {
int dt = getdt(t);
int l = 1, r = dt - 1, res = dt;
while (l <= r) {
int mid = l + (r - l) / 2;
ll nt = t - sum(mid, dt - 1);
if (nt <= 0 || getdt(nt) != mid) l = mid + 1;
else res = mid, r = mid - 1;
}
if (t - sum(res, dt - 1) <= s) {
int l = res, r = dt - 1, pos = l;
while (l <= r) {
int mid = l + (r - l) / 2;
if (t - sum(mid, dt - 1) <= s) pos = mid, l = mid + 1;
else r = mid - 1;
}
t -= sum(pos, dt - 1);
ans += (dt - 1 - pos + 1);
break;
}
t -= sum(res, dt - 1); ans += dt - res;
dt = getdt(t);
l = 1; r = dt - 2; res = dt - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
ll nt = t - sum(mid, dt - 2);
if (nt <= 0 || getdt(nt) != mid + 1) l = mid + 1;
else res = mid, r = mid - 1;
}
if (t - sum(res, dt - 2) <= s) {
int l = res, r = dt - 2, pos = l;
while (l <= r) {
int mid = l + (r - l) / 2;
if (t - sum(mid, dt - 2) <= s) pos = mid, l = mid + 1;
else r = mid - 1;
}
t -= sum(pos, dt - 2);
ans += (dt - 2 - pos + 1) * 2ll;
break;
}
t -= sum(res, dt - 2); ans += (dt - res - 1) * 2;
if (t - dt + 2 > 0 && getdt(t - dt + 2) == dt - 2) {
t -= (dt - 2); ++ans;
}
}
ans += s - t;
printf("%lld\n", ans);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3852kb
input:
2 5 1 1 5
output:
4 3
result:
ok 2 number(s): "4 3"
Test #2:
score: 0
Accepted
time: 71ms
memory: 3920kb
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