【HDU 6441】Find Integer 费马大定理+勾股数
Find Integer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1204 Accepted Submission(s): 344
Special Judge
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;(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的值。
根据费马大定理:
费马大定理,又被称为“费马最后的定理”,由17世纪法国数学家皮耶·德·费玛提出。
它断言当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解。
——来自 百度百科·费马大定理
所以当n>2时直接输出 -1 -1 即可。
n==0时,a^0+b^0=1+1=2 ≠ c^0=1,同样输出 -1 -1;
n==1时,满足a+b=c 即可。(当a==3时,3+4=7或者3+1=4都可以ac。)
n==2时,即勾股定理。相关公式为:
当a为大于1的奇数2n+1时,b=2n^2+2n, c=2n^2+2n+1
当a为大于4的偶数2n时 , b=n^2-1, c=n^2+1
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define closeio std::ios::sync_with_stdio(false)
ll s1[40005],s2[40005];
int main()
{
int t,i;
ll n,a,m;
for(i=3; i<=40000; i++)
{
if(i%2==1)
{
m=(i-1)/2;
s1[i]=2*m*m+2*m;
s2[i]=2*m*m+2*m+1;
}
else
{
m=i/2;
s1[i]=m*m-1;
s2[i]=m*m+1;
}
}
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&a);
if(n==0||n>2)
printf("-1 -1\n");
else if(n==1)
printf("%lld %lld\n",a+1,2*a+1);
else if(n==2) {
printf("%lld %lld\n",s1[a],s2[a]);
}
}
return 0;
}