【hdu】6441 Find Integer - 费马大定理

Find Integer

 

题解:

根据费马大定理很容易知道当 n>2 时,等式 a^n+b^n=c^n 是无整数解的。

再假设当 n=0 时,a^0=1。题目中说到 (1≤b,c≤1000,000,000) ,所以此时等式也是无解的。

所以,我们只需要再分 n=1 和 n=2 两种情况讨论即可。

当 n=1 时,这里就不说了。

当 n=2 时,又分为奇偶两种情况。

①奇数时,我们设 c=b+1,代入式子化简可得 2*b+1=a^2 ,利用这条式子可以求出b,c。

②偶数时,我们设 c=b+2,同理可求出b,c。

代码:

#include <cstdio>
using namespace std;
int main(){
	int t;
	int n, a;
	scanf("%d", &t);
	while(t--){
		scanf("%d %d", &n, &a);
		if(n==1)
			printf("1 %d\n", a+1);
		else if(n==2){
			if(a&1){
				int b = (a*a-1)>>1;
				printf("%d %d\n", b, b+1);
			}
			else {
				int b = (a*a-4)>>2;
				printf("%d %d\n", b, b+2);
			}
		}
		else 
			printf("-1 -1\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值