时间限制: 1 Sec 内存限制: 128 MB
提交: 16 解决: 9
[提交][状态][讨论版]
题目描述
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,求两个数的平方和等于n的种类数 例如:R(1)=4 (0,1) (0,-1) (1,0) (-1,0) 这是四种
分析:
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;
}