Circle Perimeter
Problem
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 x 2 + y 2 x_2+y_2 x2+y2的平方根.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 ) (1≤t≤1000) (1≤t≤1000) — the number of test cases.
The only line of each test case contains a single integer r r r ( 1 ≤ r ≤ 1 0 5 ) (1≤r≤10^5) (1≤r≤105).
The sum of r over all test cases does not exceed 1 0 5 10^5 105.
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 r ≤ d < r + 1 r≤d<r+1 r≤d<r+1.
Example
Input
6
1
2
3
4
5
1984
Output
8
16
20
24
40
12504
Note
The points for the first three test cases are shown below.
Code
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <sstream>//整型转字符串
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll r;
cin>>r;
ll y=r,d=0;
for(ll x=0;x<=r;x++)//枚举横坐标x,寻找y满足条件
{
while(x*x+y*y>=(r+1)*(r+1)) y--;
ll dy=y;
while(dy>0&&x*x+dy*dy>=r*r)
{
d++;
dy--;
}
}
cout<<d*4<<endl;//四个方向
}
return 0;
}