2018中国大学生程序设计竞赛 - 网络选拔赛(hdu6441 Find Integer)


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

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

Input
one line contains one integer T;(1≤T≤1000000)

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

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

else print two integers -1 -1 instead.
 

Sample Input
1
2 3
 

Sample Output
4 5

题意:a^n+b^n=c^n,给出a,n要求一组特解b,c。

思路:这其实就是赤裸裸的费马大定理,根据费马大定理。

当n>2或n=0时无解

当n=1时,特好构造c=a+1,b=1

当n=2时,利用勾股数构造出解。其实也可以暴力

代码:

int main(){
    long long t;
    long long a,b,c,n;
    long long i;
    scanf("%lld",&t);
    while(t--){
        scanf("%lld%lld",&n,&a);
        if(n==0){
            puts("-1 -1");
        }
        else if (n==1){
            printf("%lld %lld\n",1,a+1);
        }
        else if (n==2){
            bool flag=0;
            for (i=1;i<=a;i++){
                if (a*a%i==0){
                    long long temp1=i;
                    long long temp2=a*a/i;
                    if ((temp1+temp2)%2==0&&(temp1-temp2)%2==0){
                        b=abs(temp1-temp2)/2;
                        c=(temp1+temp2)/2;
                        if (b>=1&&b<=1000000000&&c>=1&&c<=1000000000){
                            printf("%lld %lld\n",b,c);
                            flag=1;
                            break;
                        }
                    }
                }
            }
            if (flag==0){
                puts("-1 -1");
            }
        }
        else {
            puts("-1 -1");
        }
  

猜你喜欢

转载自blog.csdn.net/snayf/article/details/82084050
今日推荐