2025-7-23专题训练总结
2025-7-23专题训练总结
开题顺序:
A
用时:
罚时:
思路:
由于
s(x) 很小,所以可以暴力枚举。方程为x^2 + ix - n = 0 ,解出x ,判断s(x) 是否等于i 即可。
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL a = 1, c, r = 1e18;
LL C(LL x) {
LL s = 0;
for (; x; s += x % 10, x /= 10);
return s;
}
int main() {
cin >> c, c = -c;
for (LL b = 0; b <= 100; b++) {
LL d = b * b - 4 * a * c, v = sqrt(d);
if (v * v != d) continue;
if (-b + v <= 0 || (-b + v) % (2 * a)) continue;
LL x = (-b + v) / (2 * a);
C(x) == b && (r = min(r, x));
}
cout << (r != 1e18 ? r : -1);
return 0;
}
B
用时:
罚时:
$2,3$:$l$ 的最小值设错。
思路:
二分答案。
要使
k 处的枕头最多,一定是向两边呈金字塔分布。分类讨论一下,是否可以用金字塔填满。用等差数列求和公式即可算出答案。
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL n, m, k;
bool C(LL c) {
LL s = 0;
s += c > n - k + 1 ? (c + c - (n - k)) * (n - k + 1) / 2 : (c + 1) * c / 2 + n - k + 1 - c;
s += c > k ? (c - 1 + c - k + 1) * (k - 1) / 2 : (c - 1) * c / 2 + k - c;
return s <= m;
}
int main() {
cin >> n >> m >> k;
LL l = 0, r = m + 1;
for (LL o; l + 1 < r;)
C(o = l + r >> 1) ? l = o : r = o;
cout << l;
return 0;
}
呃式子写的有点长其实可以更长。
C
用时:unsigned long long,最后还是 wyy 大佬调出来的)。
罚时:
思路:
二分答案。
二分工作时长
s ,则第i 个机器生产的文件数量为c_i = \lfloor \frac{s}{t_il_i + w_i} \rfloor \cdot l_i + \frac{\min\{s \bmod (t_il_i + w_i),~ t_il_i\}}{t_i} 。判断\sum\limits_{i = 1}^nc_i 是否超过k 即可。
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = unsigned long long;
const int N = 105;
LL t, n, k;
array<LL, 3> a[N];
bool C(LL s) {
LL c = 0;
for (int i = 1; i <= n; i++) {
auto [t, l, w] = a[i];
c += s / (t * l + w) * l + min((s % (t * l + w)) / t, l);
}
return c >= k;
}
int main() {
for (cin >> t; t; t--) {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i][0] >> a[i][1] >> a[i][2];
LL l = 0, r = 9e18;
for (LL o; l + 1 < r;)
C(o = l + r >> 1) ? r = o : l = o;
cout << r << "\n";
}
return 0;
}
D
用时:
罚时:
思路:
周期问题,计算一个周期内能烤几分钟,然后就做完了。
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL k, d, t, n, r;
int main() {
cin >> k >> d >> t, n = ((k - 1) / d + 1) * d, r = 2 * t % (n + k);
cout << fixed << setprecision(9) << 2 * t / (n + k) * n + (r <= 2 * k - 1 ? r / 2.0 : r - k);
return 0;
}
E
用时:
罚时:
思路:
二分答案。
太板了,没什么好说的了。
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e5 + 5;
int n, m, t[N];
LL a[N], b[N], c[N];
bool C(int k) {
LL x = 0, y = 0;
copy(a + 1, a + m + 1, b + 1);
for (int i = 1; i <= k; i++) b[t[i]] -= c[i];
for (int i = 1; i <= m; i++)
x += max(b[i], 0ll) / 3 * 3, y += max(-b[i], 0ll);
return x >= 3 * y;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> t[i] >> c[i];
int l = -1, r = n + 1;
for (int o; l + 1 < r;)
C(o = l + r >> 1) ? l = o : r = o;
cout << l;
return 0;
}
G
用时:
罚时:
思路:
二分答案。
二分最大距离,算出每个圆与直线的交点区间,判断是否有交集即可。
设点
(x_0, y_0) ,直线y = kx + h ,圆(x - x_0) ^ 2 + (y - y _ 0) ^ 2 = d ^ 2 。\ 可以列出方程:(k ^ 2 + 1)x ^ 2 + (2kh - 2x_0 - 2ky_0)x + x_0 ^ 2 + y_0 ^ 2 - 2y_0h + h ^ 2 - d ^ 2 = 0 解出来即可。
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e5 + 5;
int t, n, a, b, c;
pair<int, int> p[N];
bool C(double d, double k, double h) {
double l = -1e9, r = 1e9;
for (int i = 1; i <= n; i++) {
auto [x, y] = p[i];
double a = k * k + 1, b = 2 * k * h - 2 * x - 2 * k * y, c = x * x + y * y - 2 * h * y + h * h - d * d;
double d = sqrt(b * b - 4 * a * c);
double x1 = (-b - d) / (2 * a), x2 = (-b + d) / (2 * a);
l = max(l, x1), r = min(r, x2);
}
return l <= r;
}
bool C(double d, double h) {
double l = -1e9, r = 1e9;
for (int i = 1; i <= n; i++) {
auto [x, y] = p[i];
double b = -2 * y, c = x * x - 2 * x * h + h * h + y * y - d * d;
double d = sqrt(b * b - 4 * c);
double x1 = (-b - d) / 2, x2 = (-b + d) / 2;
l = max(l, x1), r = min(r, x2);
}
return l <= r;
}
int main() {
for (cin >> t; t; t--) {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> p[i].first >> p[i].second;
cin >> a >> b >> c;
if (!b) {
double h = -c * 1.0 / a, l = 0, r = 1e9;
for (int i = 1; i <= n; i++)
l = max(l, fabs(p[i].first - h));
for (double o; r - l > 1e-7;)
C(o = (l + r) / 2, h) ? r = o : l = o;
cout << fixed << setprecision(7) << r << "\n";
} else {
double k = -a * 1.0 / b, h = -c * 1.0 / b;
double l = 0, r = 1e9;
for (int i = 1; i <= n; i++) {
auto [x, y] = p[i];
l = max(l, fabs(k * x - y + h) / sqrt(k * k + 1));
}
for (double o; r - l > 1e-7;)
C(o = (l + r) / 2, k, h) ? r = o : l = o;
cout << fixed << setprecision(7) << r << "\n";
}
}
return 0;
}
需要特判
H
用时:
罚时:
思路:
二分答案。
二分最大不满意值
v ,则第i 座城市可容纳d_i = \lfloor \frac{v}{c_i} \rfloor 。若
\forall i \in [1, n],\sum\limits_{j = 1} ^ n [a_i \not= j]d_i \ge b_i 且\sum\limits_{i = 1} ^ n d_i \ge \sum\limits_{i = 1} ^ n b_i ,则有解。
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 2e5 + 5;
int t, n, a[N];
LL b[N], c[N], d[N], w;
bool C(LL v) {
LL s = 0;
for (int i = 1; i <= n; i++) s += (d[i] = v / c[i]);
for (int i = 1; i <= n; i++)
if (s - d[a[i]] < b[i]) return 0;
return s >= w;
}
int main() {
for (cin >> t; t; t--) {
cin >> n, w = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i], w += b[i];
for (int i = 1; i <= n; i++) cin >> c[i];
LL l = -1, r = 1e18;
for (LL o; l + 1 < r;)
C(o = l + r >> 1) ? r = o : l = o;
cout << r << "\n";
}
return 0;
}
I
用时:
罚时:
$2$:将错误 $1$ 修改后忘测样例,导致 RE。
思路:
二分答案。
- 计算
2025 年到哪一年有k 个平年,可以二分目标年份。- 计算
2025 到n 年的平年数量和k 的关系,可以计算1 到n 年的平年数量和k + 1533 的关系(1 到2024 年有1533 个平年)。$$\sum\limits_{i = 0}^8 \lfloor \frac{n}{4 \times 100 ^ i} \rfloor - \lfloor \frac{n}{100 ^ {i + 1}} \rfloor$$
代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL t, k, p[10];
bool C(LL n) {
LL m = 0;
for (int i = 0; i <= 8; i++)
m += n / (4 * p[i]) - n / p[i + 1];
return n - m >= k;
}
int main() {
for (int i = p[0] = 1; i <= 9; i++) p[i] = p[i - 1] * 100;
for (cin >> t; t; t--) {
cin >> k, k += 1533;
LL l = k, r = 2 * k + 1;
for (LL o; l + 1 < r;)
C(o = l + r >> 1) ? r = o : l = o;
cout << r << "\n";
}
return 0;
}