'commit'
This commit is contained in:
54
TangDou/CSP-J/J2-2021/AcWing1221_ErFen_2.cpp
Normal file
54
TangDou/CSP-J/J2-2021/AcWing1221_ErFen_2.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
const int N = 5e6 + 10;
|
||||
|
||||
struct Node {
|
||||
int c, d, sum;
|
||||
bool operator<(const Node &t) const {
|
||||
if (sum != t.sum) return sum < t.sum;
|
||||
if (c != t.c) return c < t.c;
|
||||
return d < t.d;
|
||||
}
|
||||
} f[N];
|
||||
|
||||
int n;
|
||||
int idx;
|
||||
|
||||
int main() {
|
||||
scanf("%d", &n);
|
||||
|
||||
// C预处理出 c^2+d^2
|
||||
for (int c = 0; c * c <= n; c++)
|
||||
for (int d = c; c * c + d * d <= n; d++)
|
||||
f[idx++] = {c, d, c * c + d * d};
|
||||
|
||||
//结构体排序
|
||||
sort(f, f + idx);
|
||||
|
||||
//枚举a^2+b^2
|
||||
for (int a = 0; a * a <= n; a++) {
|
||||
for (int b = a; a * a + b * b <= n; b++) {
|
||||
int t = n - a * a - b * b;
|
||||
|
||||
//手写二分模板,左闭右开
|
||||
// STL二分缺点:
|
||||
// 1、常数较大,速度慢
|
||||
// 2、对于结构体二分,需要构造空的Struct,还需要有一些玄学的赋零操作,不推荐
|
||||
//结论:全面采用手写二分办法,忘记STL的二分写法
|
||||
int l = 0, r = idx;
|
||||
while (l < r) {
|
||||
int mid = (l + r) >> 1;
|
||||
if (f[mid].sum >= t)
|
||||
r = mid;
|
||||
else
|
||||
l = mid + 1;
|
||||
}
|
||||
if (f[l].sum == t) {
|
||||
cout << a << ' ' << b << ' ' << f[l].c << ' ' << f[l].d << ' ' << endl;
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user