题解: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;
}
相关推荐
lvwangshu19 小时前
P5324 [BJOI2019] 删数 题解
线段树·题解·性质题
Tisfy2 天前
LeetCode 3876.构造奇偶一致的数组 II:三种情况分类讨论(其实还是脑筋急转弯)
算法·leetcode·题解·脑筋急转弯
lvwangshu2 天前
P7668 [JOI 2018 Final] 团子制作 / Dango Maker 题解
动态规划·题解·贪心·性质题·思维好题
lvwangshu3 天前
P6534 [COCI 2015/2016 #1] UZASTOPNI 等差树列 题解
动态规划·图论·题解·性质题
Tisfy4 天前
LeetCode 2058.找出临界点之间的最小和最大距离:遍历+遇到极值则更新(这种题谁空间复杂度不是O(1)啊)
linux·数据库·leetcode·链表·题解·模拟·遍历
Tisfy9 天前
LeetCode 1927.求和游戏:抵消+看最值
java·leetcode·游戏·题解·博弈论
Tisfy11 天前
LeetCode 1386.安排电影院座位:哈希表+位运算
算法·leetcode·散列表·题解·哈希表
初夏睡觉14 天前
原创题目 - 空壳恋爱
c++·算法·dp·oi·信息学·原创题目·值得挑战的题目
nike0good16 天前
April Fools Day Contest 2026 题解
算法·题解·愚人节比赛
Tisfy18 天前
LeetCode 3471.找出最大的几近缺失整数:三种情况判断
算法·leetcode·题解·分类讨论