题解: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;
}
相关推荐
Tisfy2 天前
LeetCode 3516.找到最近的人:计算绝对值大小
数学·算法·leetcode·题解
Tisfy2 天前
LeetCode 3027.人员站位的方案数 II:简单一个排序O(n^2)——ASCII图解
leetcode·题解·思维·排序·hard
hansang_IR4 天前
【题解】洛谷P1776 宝物筛选 [单调队列优化多重背包]
c++·算法·动态规划·题解·背包·多重背包·单调队列
hansang_IR6 天前
【题解 | 两种做法】洛谷 P4208 [JSOI2008] 最小生成树计数 [矩阵树/枚举]
c++·算法·dfs·题解·枚举·最小生成树·矩阵树定理
hansang_IR10 天前
【算法速成课1 | 题解】洛谷P3366 【模板】最小生成树 MST(Prim & Kruskal)
c++·笔记·算法·题解·最小生成树·kruskal·prim
吃奶酪的猫13 天前
Self-Adjusting Top Tree
技术·oi
Tisfy1 个月前
LeetCode 2411.按位或最大的最小子数组长度:一次倒序遍历
数据结构·算法·leetcode·题解·位运算·遍历
吃奶酪的猫1 个月前
浅谈后缀自动机
技术·oi
Tisfy1 个月前
LeetCode 1695.删除子数组的最大得分:滑动窗口(哈希表)
算法·leetcode·散列表·题解·双指针·滑动窗口·哈希表
Tisfy2 个月前
LeetCode 3202.找出有效子序列的最大长度 II:取模性质(动态规划)
算法·leetcode·动态规划·题解·模运算