HDU 6441 Find Integer (2018 CCPC网络赛)
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 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
Source
2018中国大学生程序设计竞赛 - 网络选拔赛
题意
T组询问,每次两个整数N、a,输出符合公式 a^n+b^n=c^n 的一对 b 和 c
(1≤T≤1000000)
(0≤n≤1000,000,000,3≤a≤40000)
(1≤b,c≤1000,000,000)
思路
易知,当N==0时,1 + 1 != 1,故一定无解、
N==1时,a + 1 = a+1,即可得出一对解
N==2时,a, b, c为直角三角形三边
常见的互质三角形三边
3、4、5
5、12、13
7、24、25
9、40、41
。、。、。
互质的三边有公式:
3+2K、4+(6+2K)*K、5+(6+2K)*K
因为a较小,所以跑一边 a 和 b,可以预处理出所有答案,c可以通过=a^2+b^2求得
注:上面的公式带入K,三边互质,其整数倍的三边也可构成直角三角形
N>=3时,根据费马大定理,一定无解
费马大定理,又被称为“费马最后的定理”,由17世纪法国数学家皮耶·德·费玛提出。
它断言当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 40010
ll h[maxn];
void init()
{
memset(h,0,sizeof(h)); //先清空
int j,n,a;
ll b,bb;
for(n=0;n<=19998;n++){ //因为a<=40000,
a=3+n+n; //公式
bb=4+(6+n+n)*n; //公式
b=bb;
for(j=a;j<=40000;j+=a){ //找出所有在符合区间范围的答案
if(!h[j]&&b<=1000000000){
h[j]=b; //3、4 //6 8
}
if(b<=40000&&!h[b]){
h[b]=(ll)j; //4、3 //8 6
}
if(b>=1000000000)
break;
b=b+bb; //每次累加
}
}
}
int main()
{
init();
int t;
scanf("%d", &t);
while(t--){
int i, a,n;
ll b,c;
scanf("%d %d", &n, &a);
if(n>=3||n==0){
printf("-1 -1\n");
continue;
}
if(n==1){
printf("1 %d\n", a+1);
continue;
}
if(!h[a]){ //无解
printf("-1 -1\n");
continue;
}
b=h[a];
c=(ll)sqrt(a*a+b*b);
printf("%lld %lld\n",b,c);
}
return 0;
}