题解:AT_abc389_d [ABC389D] Squares in Circle

假定原点为圆心。

我们只考虑点在第一象限的情况,剩下的情况同理。

因为圆心是原点,所以在圆内的点的横坐标一定在 \(r\) 之内。

枚举点的横坐标 \(x + \frac{1}{2}\),二分最大的 \(y + \frac{1}{2}\),使得点 \((x + \frac{1}{2}, y + \frac{1}{2})\) 到原点的距离 \(\le r\) (因为我们令圆心为原点,所以所有的点都应平移一段)。

此时所有横坐标为 \(x + \frac{1}{2}\) 的在圆内的点分别是:

\(x + \\frac{1}{2}, \\frac{1}{2}),(x + \\frac{1}{2}, 1 + \\frac{1}{2}),(x + \\frac{1}{2}, 2 + \\frac{1}{2}),\\dots,(x + \\frac{1}{2}, y + \\frac{1}{2}) \\

一共是 \(y + 1\) 个。

将枚举的所有 \(x\) 算出来的答案加起来记为 \(t\)。

因为我们只考虑了第一象限,所以答案是 \(4 \times t + 1\)(需要考虑原点的情况,所以要 + 1)。

CPP 复制代码
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define FRE(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
#define ALL(x) x.begin(), x.end()
using namespace std;

inline void cmax(int& x, int c) {
    x = max(x, c);
}
inline void cmin(int& x, int c) {
    x = min(x, c);
}

int _test_ = 1;

int n, ans; 

double dis(double x, double y) {
    return (double)sqrt((double)((double)((double)x + 0.5) * (double)((double)x + 0.5) + (double)((double)y + 0.5) * (double)((double)y + 0.5)));
}

void init() {}

void clear() {}

void solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int l = 0, r = n, t = 0;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (dis(i, mid) <= (double)n) {
                t = mid;
                l = mid + 1;
            } else
                r = mid - 1;
        }
        ans += t + 1;
    }
    cout << ans * 4 + 1;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    // cin >> _test_;
    init();
    while (_test_--) {
        clear();
        solve();
    }
    return 0;
}
相关推荐
王老师青少年编程20 小时前
2026年6月GESP真题及题解(C++五级):排排坐
c++·题解·真题·gesp·五级·2026年6月·排排坐
王老师青少年编程1 天前
2026年6月GESP真题及题解(C++五级):晚宴
c++·题解·真题·gesp·五级·2026年6月·晚宴
Tisfy4 天前
LeetCode 1358.包含所有三种字符的子字符串数目:滑动窗口(两种写法直接推荐方法二)
算法·leetcode·字符串·题解·滑动窗口
Tisfy20 天前
LeetCode 2095.删除链表的中间节点:两次遍历 / 一次遍历(快慢指针)
算法·leetcode·链表·题解·双指针
Tisfy21 天前
LeetCode 2130.链表最大孪生和:转数组 / 快慢指针+链表翻转(O(1))
算法·leetcode·链表·题解
Tisfy21 天前
LeetCode 3838.带权单词映射:求和、取模、拼接(附python一行版)
python·算法·leetcode·字符串·题解·模拟·取模
Tisfy1 个月前
LeetCode 3689.最大子数组总值 I:What The Medium
算法·leetcode·题解·贪心·模拟·脑筋急转弯
cpp_25011 个月前
P2947 [USACO09MAR] Look Up S
数据结构·c++·算法·题解·单调栈·洛谷
cpp_25011 个月前
P11375 [GESP202412 六级] 树上游走
数据结构·c++·算法·题解·洛谷·树形结构·gesp六级
cpp_25011 个月前
P10722 [GESP202406 六级] 二叉树
数据结构·c++·算法·题解·洛谷·树形结构·gesp六级