Fermat's last theorem (number theory) ------ HDU 6441 Find Integer

Find Integer

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

give you two integers n n , a a ,you are required to find 2 2 integers b b , c c such that a n + b n = c n a^{n}+b^{n}=c^{n} .

Input

one line contains one integer T T ; ( 1 T 1000000 ) (1 \ leq T \ leq 1000000)

next T T lines contains two integers n n , a a ; ( 0 n 1000000000 , 3 a 40000 0 \ leq n \ leq 1000000000,3 \ leq a \ leq 40000 )

Output

print two integers b b , c c if b b , c c exits; ( 1 b , c 1000000 1 \leq b,c \leq 1000000 );
else print two integers -1 -1 instead.

Sample Input
1
2 3
Sample Output
4 5

answer

a n + b n = c n a^{n}+b^{n}=c^{n} , gives n , a n,a , obtained b , c b,c
When n 3 n = 0 n \ geq 3 || n = 0 by Fermat's last theorem shows that no solution.
when n = 1 n = 1 , the take b = 1 , c = a + 1 b = 1,c = a + 1 to
when n = 2 n = 2 , the
if a a is an odd number, b = a 2 1 2 , c = a 2 + 1 2 b = \frac{a^{2}-1}{2},c = \frac{a^{2}+1}{2}
If the a a is an even number, b = a 2 4 1 , c = a 2 4 + 1 b = \frac{a^{2}}{4}-1,c = \frac{a^{2}}{4}+1

C ++ code

#include <iostream>

using namespace std;

void solve(int n,int a){
    if(n > 2 || n == 0){
        printf("%d %d\n",-1,-1);
    }else if(n == 1){
        printf("%d %d\n",1,a + 1);
    } else if(n == 2){
        if(a % 2 == 1)
            printf("%d %d\n",(a * a - 1)/2,(a * a + 1)/2);
        else {
            if (a == 2)
                printf("%d %d\n",-1,-1);
            else
                printf("%d %d\n", (a * a / 4) - 1, (a * a / 4) + 1);
        }
    }
}

int main(){

    int t;
    cin >>t;
    for(int i = 0;i < t;i++){
        int n,a;
        scanf("%d%d",&n,&a);
        solve(n,a);
    }

    return 0;
}
Published 52 original articles · won praise 5 · Views 2276

Guess you like

Origin blog.csdn.net/advjj_058229/article/details/100023470