CF1971F Circle Perimeter

题目

Given an integer r, find the number of lattice points that have a Euclidean distance from (0,0) greater than or equal to r but strictly less than r+1.

A lattice point is a point with integer coordinates. The Euclidean distance from (0,0) to the point (x,y) is x2+y2.
https://codeforces.com/contest/1971/problem/F

Input

The first line contains a single integer t (1t1000) — the number of test cases.The only line of each test case contains a single integer r (1r105).
The sum of r over all test cases does not exceed 105.

6
1
2
3
4
5
1984

Output

For each test case, output a single integer — the number of lattice points that have an Euclidean distance d from (0,0) such that rd<r+1.

8
16
20
24
40
12504

题解

解题思路

注意到数据范围为1e5级别,需要线性时间复杂度,故考虑枚举一个数并快速算出另外一个数
对公式rx2+y2r+1进行变形,可以得知需要枚举x(范围为(r,r)),利用公式求出y从而快速计算,注意边界情况:x2+y2(r+1)21

Code

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
using namespace std;
typedef long long LL;
signed main(){
	IOS;
    int _;cin>>_;
    while(_--){
        LL n;cin>>n;
        LL ans=0;
        for(LL i= -n;i<=n;i++){
            LL t1=ceil(sqrtl(n*n-i*i));
            LL t2=sqrtl((n+1)*(n+1)-i*i-1);
            ans+=(t2-t1+1)*2-(t1==0);
        }
        cout<<ans<<endl;
    }
	
	return 0;
}

posted on   TaopiTTT  阅读(51)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示