hdu6441 Find Integer

Discription
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 a^n + b^n= c^n.

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

题意
给定两个数n,a,求a^n + b^n= c^n中b和c的值;
若不存在则输出-1和-1

思路
这道题跟勾股数有关系。
首先能知道,当n大于2的时候这个式子无解。
所以n只有三种情况0,1,2.
当n=0时,无解;
当n=1时,令b=a,c=2a;
当n=2时,就需要用到勾股数了
本原勾股数组
当a为奇数时,令s=2
x+1;

b=2*x*x+2*x;
c=2*x*x+2*x+1;

当a为偶数时,s=2*x;
通过打表找规律得

b=x*x-1;
c=x*x+1;

AC代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
int t,n,a;

LL qmi(LL m, LL k, LL p)
{
    LL res = 1 % p, t = m;
    while (k)
    {
        if (k&1)
            res = res * t % p;
        t = t * t % p;
        k >>= 1;
    }
    return res;
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&a);
        int f=0;
        if(n==0||n>2)
            printf("-1 -1\n");
        else if(n==1)
            printf("%d %d\n",a,a*2);
        else
        {
            if(a%2)
            {
                int x=(a-1)/2;
                int b=2*x*x+2*x;
                int c=2*x*x+2*x+1;
                printf("%d %d\n",b,c);
            }
            else
            {
                int x=a/2;
                int b=x*x-1;
                int c=x*x+1;
                printf("%d %d\n",b,c);
            }
        }
    }
    return 0;
}
发布了306 篇原创文章 · 获赞 105 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43460224/article/details/104062181
今日推荐