令 。
题目大意
给定一个正整数 ,你需要构造两个正整数 ,使 成立。若无解则输出 CCF,保证无多组解。
部分分 1
注意到 是 的,即 只有 种可能的取值。
于是对于每组数据,我们可以枚举 ,对于每个 二分 ,来校验是否有 满足 。
代码
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::cerr;
template <typename T>
using U = std::make_unsigned_t<T>;
using ll = long long;
using i128 = __int128_t;
using u128 = __uint128_t;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int q;
cin >> q;
while (q--) {
ll n;
cin >> n;
auto g = [&](int n) {
int sum = n * (n + 1) >> 1;
return sum + (ll)sum * sum;
};
for (int z = 1; (n -= z * z + (ll)z * z * z * z) >= 2; z++) {
int l = 1, r = z - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (g(mid) <= n) {
l = mid + 1;
} else {
r = mid - 1;
}
}
if (g(r) == n) {
cout << r << ' ' << z - r << '\n';
break;
}
}
if (n < 2) {
cout << "CCF\n";
}
}
}时间复杂度 ,预计得分 分。
部分分 2
在处理数据之前,我们可以枚举 ,并将所有 带着 存进一个数组。
将该数组按 排序后,对每组数据进行一次关于该数组的二分查找即可。
代码
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::cerr;
template <typename T>
using U = std::make_unsigned_t<T>;
using ll = long long;
using i128 = __int128_t;
using u128 = __uint128_t;
struct state {
ll val;
int x, y;
state(ll val, int x, int y) : val(val), x(x), y(y) {}
bool operator<(const state &x) const {
return val < x.val;
}
};
std::vector<state> ans;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int q;
cin >> q;
constexpr int Z = 1379;
ans.reserve(Z * (Z - 1) >> 1);
auto f = [&](int n) {
return (ll)n * (n + 1) * (2 * n + 1) / 6 + (i128)n * (n + 1) * (2 * n + 1) * (3 * n * n + 3 * n - 1) / 30;
};
auto g = [&](int n) {
int sum = n * (n + 1) >> 1;
return sum + (ll)sum * sum;
};
for (int z = 2; z <= Z; z++) {
ll fz = f(z);
for (int x = 1; x < z; x++) {
ll sum = fz + g(x);
if (sum <= (ll)1e15) {
ans.emplace_back(sum, x, z - x);
} else {
break;
}
}
}
std::sort(ans.begin(), ans.end());
while (q--) {
ll n;
cin >> n;
auto it = std::lower_bound(ans.begin(), ans.end(), state(n, 0, 0));
if (it != ans.end() && it->val == n) {
cout << it->x << ' ' << it->y << '\n';
} else {
cout << "CCF\n";
}
}
}时间复杂度 ,预计得分 分。若特判数据点,再结合“部分分 1”的代码,则能够得到 分。
正解
注意到 。
证明
要证该式,只需证更强的:
展开函数得:
展开平方并加以整理得:
要证该式,只需证更强的:
等价于:
证毕。
因此,只有最大且满足 的 才可能满足 。
于是对于每组数据,我们可以二分 ,然后二分 ,来校验是否有 满足 。
代码
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::cerr;
template <typename T>
using U = std::make_unsigned_t<T>;
using ll = long long;
using i128 = __int128_t;
using u128 = __uint128_t;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int q;
cin >> q;
while (q--) {
ll n;
cin >> n;
auto f = [&](int n) {
return (ll)n * (n + 1) * (2 * n + 1) / 6 + (i128)n * (n + 1) * (2 * n + 1) * (3 * n * n + 3 * n - 1) / 30;
};
auto g = [&](int n) {
int sum = n * (n + 1) >> 1;
return sum + (ll)sum * sum;
};
auto bs = [&](int l, int r, auto calc) {
while (l <= r) {
int mid = (l + r) >> 1;
if (calc(mid) <= n) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return r;
};
constexpr int Z = 5492;
int z = bs(2, Z, [&](int x) {
return f(x) + 2;
});
n -= f(z);
int x = bs(1, z - 1, g);
if (g(x) == n) {
cout << x << ' ' << z - x << '\n';
} else {
cout << "CCF\n";
}
}
}时间复杂度 ,预计得分 分。