题解:CF2253D Hypercarp and Interdimensional Jumps
Subway_hub · · 题解
非常有意思的数学题!
首先也是最重要的转化:我们考虑假设现在是第
所以题目转化为:
既然这样,我们观察到
所以贪心来看,我们要求最大的
再看题目要求的与终点的最短距离:
转化:
根据这是一个关于
但
最后我们要处理
这样我们就做完了。
#include<bits/stdc++.h>
using namespace std;
namespace CZW {
#define endl "\n"
#define vec std::vector
#define pb push_back
#define eb emplace_back
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using lb = long double;
void Main() {
ll x, y;
cin >> x >> y;
ll n = sqrtl(2.0 * (x + y));
while ((n + 1) * (n + 2) / 2 <= x + y) ++n;
while (n * (n + 1) / 2 > x + y) --n;
ll s = n * (n + 1) / 2;
lb pi = (x - y + s) / 2.0;
ll p1 = floor(pi), p2 = ceil(pi);
p1 = max(max(0ll, s - y), min(min(s, x), p1));
p2 = max(max(0ll, s - y), min(min(s, x), p2));
auto get = [&](ll p)->ll{
ll q = s - p;
return (x - p) * (x - p) + (y - q) * (y - q);
};
ll best = p1;
if (get(p2) < get(p1)) best = p2;
vec<char> ans(n + 1, 'Y');
ll cur = best;
for (ll i = n; i >= 1; --i) {
if (cur >= i) {
cur -= i;
ans[n - i + 1] = 'X';
}
}
for (int i = 1; i <= n; ++i) cout << ans[i];
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Test = 1;
cin >> Test;
while (Test--) CZW::Main();
return 0;
}