版本比较
题解:等式 / equation
标题
增加 1 行删除 1 行
11题解:T736760 复杂的等式 / Complex equation题解:等式 / equation正文
增加 88 行删除 107 行
11234567891011111213141617151617181920212526272829223132333423242526272829303136373839404142434445464748495051525354323334353637383940414243444546474849505156525354552 行未改动5859606167626364657273666768692 行未改动72737475847677788779808182838485949596979899100101102861041058788899091929394959697989910010110211110310410510610710810911011912012112212312412512612712812913011111211311411511611711811912013112112213412312412512612713912812913013113214 行未改动147148149150162151152153154167168155156157158159160161176177178179180181182183184162186187188189163164165166167191192193194195196197198199200201202203204205206207208209210211212213214215168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198217199200201令 $z=x+y,\ f(x)=\sum\limits_{k=1}\limits^{x}k+\sum\limits_{k=1}\limits^{x}k^3,\ g(x)=\sum\limits_{k=1}\limits^{x}k^2+\sum\limits_{k=1}\limits^{x}k^4$。令 $z=x+y,\ f(x)=\sum\limits_{k=1}\limits^{x}k^2+\sum\limits_{k=1}\limits^{x}k^4,\ g(x)=\sum\limits_{k=1}\limits^{x}k+\sum\limits_{k=1}\limits^{x}k^3$。## 题目大意给定一个正整数 $n$,你需要构造两个正整数 $x,y$,使 $f(z)+g(x)=n$ 成立。若无解则输出 `CCF`,保证无多组解。## 部分分 1注意到 $f(x)$ 是 $O(x^5)$ 的,即 $z$ 只有 $O(n^{\frac{1}{5}})$ 种可能的取值。
于是对于每组数据,我们可以枚举 $z$,对于每个 $z$ 二分 $x\in[1,z)$,来校验是否有 $x$ 满足 $g(x)=n-f(z)$。### 代码```cpp#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--) { 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; 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"; } }; 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;}```时间复杂度 $O(Tn^{\frac{1}{5}}\log n)$,预计得分 $20$ 分。## 部分分 22 行未改动将该数组按 $f(z)+g(x)$ 排序后,对每组数据进行一次关于该数组的二分查找即可。 ### 代码```cpp#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;2 行未改动
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) { bool operator<(const state &x) const { 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; 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 <= 1e15) { if (sum <= (ll)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"; } } 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"; } } return 0;}```时间复杂度 $O((n^{\frac{2}{5}}+T)\log n^{\frac{2}{5}})$,预计得分 $30$ 分。若特判数据点,再结合“部分分 1”的代码,则能够得到 $50$ 分。时间复杂度 $O((n^{\frac{2}{5}}+T)\log n)$,预计得分 $30$ 分。若特判数据点,再结合“部分分 1”的代码,则能够得到 $50$ 分。## 正解注意到 $f(z)+g(x)<f(z+1)+g(1)$。::::info[证明]要证该式,只需证更强的:$$g(x)<f(x+1)-f(z)$$$$g(x)<f(z+1)-f(z)$$展开函数得:$$\frac{x^2+x}{2}+(\frac{x^2+x}{2})^2<z^4+z^2$$展开平方并加以整理得:$$\frac{1}{4}x^4+\frac{1}{2}x^3+\frac{3}{4}x^2+\frac{1}{2}x<z^4+z^2$$14 行未改动于是对于每组数据,我们可以二分 $z$,然后二分 $x\in[1,z)$,来校验是否有 $x$ 满足 $g(x)=n-f(z)$。### 代码```cpp#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--) { cin.tie(nullptr)->sync_with_stdio(false); int q; cin >> q; while (q--) { 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"; } 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"; } } return 0;}```时间复杂度 $O(T\log n)$,预计得分 $100$ 分。