题目:
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.
题意:
给你两个数,n是指数,a是底数,求b,c满足 a^n+b^n=c^n。
实际上就是费马大定理,在n>2时,没有整数成立,=2就是勾股数,这里考察了构造勾股数,
本来用公式的话:
a为大于1的奇数2n+1,b = 2n^2+2n,c = b+1;
a为大于4的偶数2n时,b = n^2-1,c = n^2+1;
这样就可以构造勾股数了,但是,这道题就T了。。。。
所以,后来通过构造本原勾股数,预处理,才通过了。
预处理代码:
void ppt(){
ll s,t;
for(s = 3; s <= 40000; s += 2){
for(t = 1; t < s; t += 2){
if(__gcd(s,t) == 1) {
ll a = s*t;
if(a > 40000)
break;
ll b = (s*s-t*t)/2;
ll c = (s*s+t*t)/2;
A[a][0] = b;
A[a][1] = c;
if(b > 40000)
break;
A[b][0] = a;
A[b][1] = c;
}
}
}
}
代码:
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int t;
ll n,m;
while(cin >> t){
cin >> n >> m;
if(n > 2)
cout << -1 << " " << -1 << endl;
else{
if(n == 0){
cout << -1 << " " << -1 << endl;
}
if(n == 1)
{
cout << 1 <<" " << m+1 << endl;
}
if(n == 2){
if(m <= 2){
cout << -1 << " " << -1 << endl;
}
else{
int x = 0;
if(m % 2){
x = (m-1)/2;
cout << 2*x*x+2*x << " " << 2*x*x+2*x+1 << endl;
}
else{
x = m/2;
cout << x*x-1 << " " << x*x+1 << endl;
}
}
}
}
}
}