题目来源: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 a, b and c, often called the Pythagorean equation:
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 a, b 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
5
1
74
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是整数,那么这个三角形肯定存在.....
如果你还怕超时,那么请进行进一步优化。剪枝.......
把需要重复计算的标记,下次再需要用的时候直接返回,不用再一步步的重新计算得出。
对的,就是这样、、、我想你已经知道怎么做了。。。。
代码如下:
本人是大水比一枚,代码如有错请积极指出。。。。