CF 1971 F. Circle Perimeter (*1600) 思维 数学
CF 1971 F. Circle Perimeter (*1600) 思维 数学
题意:
找出平面直角坐标系中所有到原点距离 \(d\) , 满足 \(r\le d <r+1\) 的所有整数坐标点。
思路:
注意到所有的都是对称出现的,因此我们只需要找出第一象限的点然后乘 \(4\) 即可。
我们可以枚举从 \(1-r\) 横坐标,然后算出纵坐标的范围即可。对于当前枚举的横坐标 \(x\)。
有 \(r \le \sqrt{x^2+y^2} < r+1\) 。平方一下得: \(r^2 \le x^2+y^2 < (r+1)^2\) 。
移项得: \(r^2-x^2 \le y^2 < (r+1)^2-x^2\) 。开根得: \(\sqrt{r^2-x^2} \le y < \sqrt{(r+1)^2-x^2}\)。
所以有 \(y_{min}=\sqrt{r^2-x^2}\) , \(y_{max}=\sqrt{(r+1)^2-x^2-eps}\)。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define all(u) u.begin(), u.end()
#define endl '\n'
#define debug(x) cout<<#x<<":"<<x<<endl;
typedef pair<int, int> PII;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10, M = 105;
const int mod = 1e9 + 7;
const int cases = 1;
const double eps = 0.5;
void Showball(){
int r;
cin>>r;
LL ans=0;
auto f=[&](LL n){return n*n;};
for(LL i=1;i<=r;i++){
LL maxn=floor(sqrt(f(r+1)-f(i)-eps));
LL minn=ceil(sqrt(f(r)-f(i)));
ans+=maxn-minn+1;
}
cout<<ans*4<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T=1;
if(cases) cin>>T;
while(T--)
Showball();
return 0;
}