HDU3835

本文介绍了一种通过编程算法计算特定整数可以由多少种不同的两个整数平方和表示的方法。该算法考虑了所有可能的组合,并分别针对完全平方数和两倍完全平方数的情况进行了额外计数。

ANS=0;

if x*x+x*x==n ANS+=4;//( x,x)  (-x,-x) (-x,x)  (x,-x)

if x*x+0*0==n ANS+=4;//(x,0)  (0,x)  (-x,0) (0,-x) 

if a*a+b*b==n ANS+=8;

View Code
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 const int maxn = 10005;
 5 int a[ maxn ];
 6 int main(){
 7     memset( a,0,sizeof(a));
 8     a[0]=1;
 9     for( int i=1;i<maxn;i++ ){
10         int tmp=i/2;
11         int now=1;
12         while( now*now<tmp ){
13             int sum=i-now*now;
14             int tp=sqrt( sum*1.0 );
15             if( tp*tp==sum ) a[ i ]+=8;
16             now++;
17         }
18         if( (int)sqrt(i*1.0)*(int)sqrt(i*1.0)==i ) a[ i ]+=4;
19         if( (int)sqrt(i*1.0)*(int)sqrt(i*1.0)*2==i ) a[ i ]+=4;
20     }
21     int n;
22     while( scanf("%d",&n)==1 ){
23         if( n<maxn ){
24             printf("%d\n",a[ n ]);
25             continue;
26         }
27         int ans=0;
28         int i=n;
29         int tmp=i/2;
30         int now=1;
31         while( now*now<tmp ){
32             int sum=i-now*now;
33             int tp=sqrt( sum*1.0 );
34             if( tp*tp==sum ) ans+=8;
35             now++;
36         }
37         if( (int)sqrt(i*1.0)*(int)sqrt(i*1.0)==i ) ans+=4;
38         if( (int)sqrt(i*1.0)*(int)sqrt(i*1.0)*2==i ) ans+=4;
39         printf("%d\n",ans);
40     }
41     return 0;
42 }

 

转载于:https://www.cnblogs.com/xxx0624/archive/2013/02/02/2890344.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值