a^2 + b^2 = c^2


题目来源:CodeForce 304

Description

In mathematics, the Pythagorean theorem — is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states:

In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle).

The theorem can be written as an equation relating the lengths of the sides ab and c, often called the Pythagorean equation:

a2 + b2 = c2

where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides.

Given n, your task is to count how many right-angled triangles with side-lengths ab and c that satisfied an inequality 1 ≤ a ≤ b ≤ c ≤ n.

Input

The only line contains one integer n (1 ≤ n ≤ 104) as we mentioned above.

Output

Print a single integer — the answer to the problem.

Sample Input

Input
5
Output
1
Input
74
Output
35

首先,我们需要理解题意(^.^  很简单,判断1《a《b《c《n中,有多少个三角形满足a^2+b^2=c^2)。勾股定理。。。。

OK,理解了题意之后,我们开始来做题。啦啦啦。。。

解题思路:

刚开始,最先想到的是暴力枚举每一个a,b,c,看是否满足勾股定理。对,这是最容易想到的方法。

但是!!!请注意n的取值范围,小于等于10000,所以暴力枚举的算法是n^3。不好意思,你已经华丽丽的TLE(超时)了。

所以,再进一步想可不可以优化到时间复杂度为n^2,甚至更小呢......

所以,我们来想只枚举c和b的值,再判断a是否存在。是不是就是n^2的复杂度了吗?

如果a是整数,那么这个三角形肯定存在.....

如果你还怕超时,那么请进行进一步优化。剪枝.......

把需要重复计算的标记,下次再需要用的时候直接返回,不用再一步步的重新计算得出。

对的,就是这样、、、我想你已经知道怎么做了。。。。

代码如下

本人是大水比一枚,代码如有错请积极指出。。。。

#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
int ans[10005],key,n;
int main()
{
    memset(ans,0,sizeof(ans));
    key=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(key>=n)
            {printf("%d\n",ans[n]);
        continue;}
        for(int i=n;i>=5;i--)
        {
            if(i==key)
            {ans[n]+=ans[key];break;}
            for(int j=i-1;j*j>=(i*i)/2;j--)
            {
                int k=sqrt(i*i-j*j);
                if(k*k+j*j==i*i)
                    ans[n]++;
            }
        }
        key=n;
        printf("%d\n",ans[n]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值