勾股数组



      一般地,若三角形三边长a,b,c都是正整数,且满足a,b的平方和等于c的平方,那么数组(a,b,c)称为勾股数组。勾股数组是人们为了解出满足勾股定理的不定方程的所有整数解而创造的概念。
      再来看下面这些勾股数:(3,4,5),(5,12,13),(7,24,25),(9,40,41),(11,60,61)…这些勾股数都是以奇数为一边构成的直角三角形。由以上已知任意一个大于2的偶数可以构成一组勾股数,实际上以任意一个大于1的奇数2n+1(n>1)为边也可以构成勾股数,其三边分别是2n+1、2n^2+2n、2n^2+2n+1,这可以通过勾股定理的逆定理获证。
                                                                                                                                        ———以上来自百度百科



      由以上已知任意一个大于2的偶数可以构成一组勾股数,实际上以任意一个大于1的奇数2n+1(n>1)为边也可以构成勾股数,其三边分别是2n+1、2n^2+2n、2n^2+2n+1,这可以通过勾股定理的逆定理获证。
       上面这句话是重点句,也就是说,我们只要能得到一个大于2的数,就可以得到一个勾股数组。下面用一道题来练习一下(2018中国大学生程序设计竞赛 - 网络选拔赛的1004题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6441


Find Integer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6597    Accepted Submission(s): 1852
Special Judge


Problem Description
people in USSS love math very much, and there is a famous math problem .

give you two integers n n ,a,you are required to find 2 2 integers b, c c such that an+ bn=cn b n = c n .
 

Input
one line contains one integer T T ;(1T1000000)

next T T lines contains two integers n, a a ;(0n1000, 000 000 , 000,3a40000) 000 , 3 ≤ a ≤ 40000 )
 

Output
print two integers b b ,c if b b ,c exits; (1b,c1000 ( 1 ≤ b , c ≤ 1000 , 000 000 , 000) 000 ) ;

else print two integers -1 -1 instead.
 

Sample Input
   
   
1 2 3
 

Sample Output
   
   
4 5
 


      这道题的题意是有一个公式a^n + b^n = c^n,输入n和a,然后求出b和c的值,没有的话输出-1 -1。首先由费马大定理可得当n大于2的时候是无解的,所以我们只需要去讨论当n等于0、1、2这三种情况就好了,当n等于0的时候易知无解,当n等于1的时候我们只需要输出1 a+1就好了。当n等于2的时候就是上面所说的要去求一个勾股数组,当a为奇数有勾股数(2*a+1,2*a*a+2*a,a*a+1);当a为偶数有勾股数(2*a,a*a-1,a*a+1)。


AC代码:

#include <bits/stdc++.h>
using namespace std;
int T,n,a;

int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&a);
                if(n > 2 || n == 0){
                printf("-1 -1\n");
            }
            else if(n == 1){
                printf("1 %d\n",a + 1);
            }
            else if(n == 2){
                if(a % 2 == 1){
                    int ans = (a - 1) / 2;
                    printf("%d %d\n",ans * ans + (ans+1) * (ans+1) - 1,ans * ans + (ans+1) * (ans+1));
                }
                else{
                    int ans = (a + 2) / 2;
                    printf("%d %d\n",(ans - 1) * (ans - 1) - 1,(ans-1) * (ans-1) + 1);
                }
            }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值