HDU 6441 Find Integer 费马大定理 + 勾股数

1.题意:给你a和n , 让你求 a^n + b^n = c^n 中b和c1,若无解输出-1,-1;若有解输出任意一组b,c

2.分析:

(1)费马大定理:它断言当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解。最终被证明!

(2)勾股数公式:

基本:a^2 + b^2 = c^2

拓展:

当a为大于1的奇数2n+1时,b=2n^2+2n, c=2n^2+2n+1。(柏拉图勾股公式)

当a为大于4的偶数2n时,b=n^2-1, c=n^2+1(毕达哥拉斯勾股公式)

当a = 3时:b = 4,c = 5

3.代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long LL;
int main()
{
   int T;
   scanf("%d",&T);
   while(T--){
      LL n,a;
      scanf("%lld%lld",&n,&a);
      if(n>2||n==0){
         printf("-1 -1\n");
      }
      else if(n==1){
        printf("1 %lld\n",a+1);
      }
      else{
        LL m = a/2;
        //cout<<m<<endl;
        LL b,c;
        if(a&1){//勾股公式
            b = 2*m*m + 2*m;
            c = 2*m*m + 2*m + 1;
        }
        else{
            b = m*m - 1;
            c = m*m + 1;
        }
        printf("%lld %lld\n",b,c);
      }
   }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40772692/article/details/82079020