题解: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;
}
相关推荐
Lyrella1 天前
luogu-P5320题解
数学·题解
炒鸡码力10 天前
一道原创OI题(普及-)——ZCS的随机游走
c++·算法·题解·模拟·题目
Tisfy15 天前
LeetCode 2272.最大波动的子字符串:转为多次的最大子数组和 - 一步步思考推导
算法·leetcode·动态规划·字符串·题解
Tisfy16 天前
LeetCode 3110.字符串的分数:模拟(注意一个小细节)
算法·leetcode·字符串·题解·模拟
Tisfy1 个月前
LeetCode 0132.分割回文串 II:动态规划
leetcode·动态规划·字符串·题解·回文
Igallta_8136221 个月前
【CCF GESP 2 级】小杨的H字矩阵 洛谷 B3924
c语言·开发语言·c++·笔记·算法·题解·信息学奥林匹克竞赛
朔北之忘 Clancy1 个月前
2022 年 12 月青少年软编等考 C 语言五级真题解析
c语言·开发语言·c++·学习·算法·青少年编程·题解
Tisfy1 个月前
LeetCode 1472.设计浏览器历史记录:一个数组完成模拟,单次操作均O(1)
服务器·leetcode·浏览器·memcached·题解·模拟·数组
lskkkkkkkkkkkk1 个月前
第16届蓝桥杯模拟赛3 python组个人题解
python·蓝桥杯·题解
Tisfy1 个月前
LeetCode 2209.用地毯覆盖后的最少白色砖块:记忆化搜索之——深度优先搜索(DFS)
算法·leetcode·深度优先·dfs·题解·记忆化搜索·深度优先搜索