[ABC389D] Squares in Circle
[ABC389D] Squares in Circle
思路
发现 \(n\) 比较小,考虑枚举每一行并二分求该行的方块数。
因为原点往上的方块数与原点往下的方块数是相等的,所以只需要枚举向上或向下的方块数,将方块数乘 \(2\) 减去中间多算的方块数即可。
设原点为 \((-0.5,-0.5)\),每次二分该行最右边方块的右上角(该点为这行距离原点最远的点之一)的 \(x\) 坐标,则该点到原点的距离为 \(\sqrt{(x+0.5)^2+(y+0.5)^2}\),通过二分求出坐标后,这行的方块数即为 \((2\times x-1)\)。
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,tot;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
cin>>n;
for(int i=0;i<n;i++){
int l=0,r=n-1,mid,res=-1;
while(l<=r){
mid=(l+r)/2;
if(sqrt((mid+0.5)*(mid+0.5)+(i+0.5)*(i+0.5))>n)
r=mid-1;
else
l=mid+1,res=mid;
}
tot+=res*2+1;
}
cout<<tot*2-2*n+1;
return 0;
}

浙公网安备 33010602011771号