Find Integer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0Special 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
题解
本题给出一个费马大定理的表达式 —— $a^n+b^n=c^n,(a,b,c) \in Z$。 题目意思是给定该公式中的a和n,求满足公式的b和c并输出,若不存在满足公式的b和c,则输出”-1 -1”。所以当n>2或n==0时输出”-1 -1”;当n=1时输出满足方程的第一组解:1 a+1;当n=2时输出勾股数。
求勾股数:这种前n项固定的情况一般可以采用打表法。三层循环肯定是不行的,这么大数据量肯定tle。$a^2=c^2-b^2=(c+b)(c-b)$。令$x=c+b,y=c-b$,于是$a^2=xy$。从x着手,当满足a2可以整除x时就可以求出符合条件的b,c。
代码如下:
1 |
|