SDNU 1147 Pythagoras's Revenge 【技巧暴力】

1147.Pythagoras's Revenge

Time Limit: 1000 MS    Memory Limit: 32768 KB
Total Submission(s): 4    Accepted Submission(s): 2

Description

The famous Pythagorean theorem states that a right triangle, having side lengths A and B and hypotenuse length C, satisfies the formula

A 2 +  B 2 =  C 2

It is also well known that there exist some right triangles in which all three side lengths are integral, such as the classic: 

Further examples, both having A=12, are the following: 

The question of the day is, given a fixed integer value for A, how many distinct integers B > A exist such that the hypotenuse length C is integral?

Input

Each line contains a single integer  A , such that  2 ≤ A < 1048576 = 2 20 . The end of the input is designated by a line containing the value 0.

Output

For each value of  A , output the number of integers  B > A  such that a right triangle having side lengths  A  and  B  has a hypotenuse with integral length.

Sample Input

3
12
2
1048574
1048575
0

Sample Output

1
2
0
1
175

Hint

Our hint is that you need not consider any value for B that is greater than  ( A 2-1)/2 , because for any such right triangle, hypotenuse C satisfies  B <  C <  B + 1 , and thus cannot have integral length.

Our warning is that for values of  A ≈ 2 20 , there could be solutions with  B ≈ 2 39 , and thus values of  C 2 >  B 2 ≈ 2 78 .

You can guarantee yourself 64-bit integer calculations by using the type long long in C++ or long in Java. But neither of those types will allow you to accurately calculate the value of C2 for such an extreme case. (Which is, after all, what makes thisPythagoras's revenge!)

Source



题目链接:

SDNU 1147 Pythagoras's Revenge


题目大意:

给定直角三角形的一直角边,求出各边均为整数的以它为最小边的直角三角形的个数


解题思路:

一、【真丶暴力】

根据勾股定理可知a^2+b^2=c^2我们试着让b从等于a+1开始枚举可能的解,如果枚举出来的c是整数的话,让ans++,最后输出ans。
然而这样枚举的终止条件是什么呢?10*a?100*a?1000*a? 我一直试到了10000*a还是求不全。 显然这样是不行的。

二、【技巧丶暴力】
对于a^2+b^2=c^2,我们移项可得a^2=c^2-b^2,再化,可得a^2=(c-b)*(c+b)。根据三角形任意两边之和大于第三边、任意两边之差小于第三边可知,令i=c-b,j=c+b,即i<a、 j>a,且j-i = 2b。接下来枚举i就可以了。

Mycode:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX = 10005;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;

int main()
{
    LL a, b, ans;
    while(scanf("%lld",&a) && a)
    {
        ans = 0;
        for(LL i = 1; i < a; ++i)
        {
            if(a * a % i == 0)
            {
                LL j = a * a / i;
                if((j-i) % 2 == 0)
                {
                    b = (j-i) / 2;
                    if(b > a)
                        ++ans;
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值