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;(1T1000000)

next T lines contains two integers n,a;(0n1000,000,000,3a40000)
 
Output
print two integers  b,c if b,c exits;(1b,c1000,000,000);

else print two integers -1 -1 instead.
 
Sample Input
1 2 3
 
Sample Output
4 5
 
Source
题目的意思是已知 n、 a 求解方程 a^n + b^n = c^n;
 从而首先由费马大定律

n>2 时 an+bn=cnan+bn=cn 没有整数解,只需要计算 n=0,1,2 这三种情况:

1、n=0,任何的正整数 b,cb,c 都无法使等式成立。

2、n=1,任意取。

3、n=2,a2=(c+b)(cb),分两种情况讨论:

      若 a为奇数,则 a2 也为奇数,则取  b=(a*a-1)/2,c=(a*a+1)/2

 若 a 为偶数,则 a2 必然是 4 的倍数,则取 b=(a*a-4)/4,c=(a*a+4)/4

#include<iostream>
#include<cstdio>
using namespace std; 
#define fi first  
#define se second 
#define mp make_pair   
#define pb push_back  
#define sz(x) ((int)(x).size())   
#define all(x) (x).begin(), (x).end()        
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
typedef long long ll;  
        
typedef pair<int,int> pii; 
//head
 
int main() {  
ll T,a,n;
  
cin>>T;

while(T--){
scanf("%lld %lld",&n,&a);
	if(n==0||n>2)
	printf("-1 -1\n");
	else if(n==1){
	printf("1 %lld",a+1);
	}
	else
	if(a&1){
		printf("%lld %lld\n",(a*a-1)/2,(a*a+1)/2);
	}
	else
printf("%lld %lld\n",(a*a-4)/4,(a*a+4)/4);
}
    return 0;  
} 

  不过还是以后可以输出输入尽量都用scanf和printf。。。

 

猜你喜欢

转载自www.cnblogs.com/Yum20/p/9616375.html
今日推荐