Find Integer HDU - 6441(费马大定理+勾股数)

Find Integer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1524    Accepted Submission(s): 516
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

 

Source

2018中国大学生程序设计竞赛 - 网络选拔赛

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

吐槽一下:比赛的时候看到这个题目的时候一下就想到了费马大定理。但是因为不够熟悉,以为给出两个数之后就会有解了。。。。还好队友翻了一下费马大定理,不然就出事了。

题意,给出a,n求出满足a^n+b^n=c^c的b,c

思路:据费马大定理,n大于2直接无解,n为0时,易证无解,n为1时随便解,n为2时,因为我只知道本原勾股数的公式

a=s*t,b=(s*s-t*t)/2,c=(s*s+t*t)/2

令t=1则b=(a^2-1)/2,c=(a^2+1)/2.但是如果a是偶数就不能得到正整数解。因此我只能把4的倍数拿出,其一点有解为3的倍数和5的倍数,当其不为4的倍数只为2的倍数时其除2定为奇数,则可以使用上公式。

当然比赛结束之后,我发现还有一个更加神奇的公式:

emmmmmm

菜哭了

AC代码:

#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
	long long a,n;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&a);
		if(n>2||n==0)
		{
			printf("-1 -1\n");
		}
		else
		{
			if(n==1)
			printf("1 %lld\n",a+1);
			else
			{
				if(!(a%4))
				{
					long long cnt = a / 4;
					printf("%lld %lld\n",3*cnt,5*cnt);
				} else if(!(a%2)){
					a = a / 2;
					printf("%lld %lld\n", (a*a-1), (a*a+1));
				}
				else
				{
					printf("%lld %lld\n",(a*a-1)/2,(a*a+1)/2);
				}
			}
		}
	}
 }

猜你喜欢

转载自blog.csdn.net/baiyifeifei/article/details/82143200