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