[提交][状态][讨论版]

We know that some positive integer x can be expressed as x=A^2+B^2(A,B are integers). Take x=10 for example,
10=(-3)^2+1^2.
We define R(N) (N is positive) to be the total number of variable presentation of N. So R(1)=4, which consists of 1=1^2+0^2, 1=(-1)^2+0^2, 1=0^2+1^2, 1=0^2+(-1)^2.Given N, you are to calculate R(N).

No more than 100 test cases. Each case contains only one integer N(N<=10^9).

For each N, print R(N) in one line.

2
6
10
25
65

4
0
8
12
16

For the fourth test case, (A,B) can be (0,5), (0,-5), (5,0), (-5,0), (3,4), (3,-4), (-3,4), (-3,-4), (4,3) , (4,-3), (-4,3), (-4,-3)

aigoruan Recommend

n的范围是[1,10^9], 枚举根号n内的数，时间复杂度O(n^1/2)

``````#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
const double INF=0x3f3f3f3f+1.0;
const double eps=1e-6;
typedef long long LL;
int main()
{
double n;
while(~scanf("%lf",&n))
{
int ans=0;
for(int i=1;i<=sqrt(n);i++)枚举的时候不能从0开始！！！ 因为当0构成平方数时会重复计算
{
int t=sqrt(n-i*i);
///          printf("i=%d t=%d\n",i,t);
if(i*i+t*t==n)
ans+=4;
}
printf("%d\n",ans);
}
return 0;
}
``````