2018 CCPC 网络赛 1004 Find Integer | 费马大定理+奇偶数列
Find Integer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6597 Accepted Submission(s): 1852
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 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
思路:
当 n > 2 时,由费马大定理,不存在正整数 a,b,c 满足 a^n + b^n == c^n ,也就是说当 n 大于 2 时,只能输出 -1 -1 。
接下来问题就可以变成 n 分别取 0,1,2 的情况了。
当 n == 1 时,由于只要输出任意一组合理解即可,则 b 为 1 ,c 为 a + 1 即可。
当 n == 0 时,条件变成了 1 + 1 == 1,无法满足,输出 -1 -1.
当 n == 2 时,条件变成了 a^2 + b^2 == c^2 也就是在已知 一个勾股数的情况下,求其他两个勾股数。
勾股数:2 * k + 1,2 * k * ( k + 1 ),2 * k * ( k + 1 ) + 1( k 为正整数 )
当 a 为奇数时,则 a = 2 * k + 1 ,解得 k 的值,则 b = 2 * k * ( k + 1 ),c = 2 * k * ( k + 1 ) + 1;
当 a 为偶数时,则 a 可能等于 p * ( 2 * k + 1 ),也可能等于 2 * k * ( k + 1 ) ,
#include <bits/stdc++.h>
int main()
{
int q;
scanf("%d",&q);
long long int a,n,b,c;
while(q--){
scanf("%lld %lld",&n,&a);
if(n==1){
printf("1 %lld\n",a+1);
}else
if(n==2){
if(a%2==0){
a=(a-2)/2;
c=1+(a+1)*(a+1);
b=c-2;
printf("%lld %lld\n",b,c);
}else if(a%2==1){
a=(a-1)/2;
c=a*a+(a+1)*(a+1);
b=c-1;
printf("%lld %lld\n",b,c);
}
} else {
printf("-1 -1\n");
}
}
}