Find Integer(费马大定律+构造勾股数)

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;(1T1000000)

next T lines contains two integers n,a;(0n1000,000,000,3a40000)
 

 

Output
print two integers  b,c if b,c exits;(1b,c1000,000,000);

else print two integers -1 -1 instead.
 

 

Sample Input
1 2 3
 

 

Sample Output
4 5
 

 

Source
题目的意思是已知 n、 a 求解方程 a^n + b^n = c^n;
 从而首先由费马大定律

n>2 时 an+bn=cnan+bn=cn 没有整数解,只需要计算 n=0,1,2 这三种情况:

1、n=0,任何的正整数 b,cb,c 都无法使等式成立。

2、n=1,任意取。

3、n=2,a2=(c+b)(cb),分两种情况讨论:

      若 a为奇数,则 a2 也为奇数,则取  b=(a*a-1)/2,c=(a*a+1)/2

 若 a 为偶数,则 a2 必然是 4 的倍数,则取 b=(a*a-4)/4,c=(a*a+4)/4

#include<iostream>
#include<cstdio>
using namespace std; 
#define fi first  
#define se second 
#define mp make_pair   
#define pb push_back  
#define sz(x) ((int)(x).size())   
#define all(x) (x).begin(), (x).end()        
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
typedef long long ll;  
        
typedef pair<int,int> pii; 
//head
 
int main() {  
ll T,a,n;
  
cin>>T;

while(T--){
scanf("%lld %lld",&n,&a);
	if(n==0||n>2)
	printf("-1 -1\n");
	else if(n==1){
	printf("1 %lld",a+1);
	}
	else
	if(a&1){
		printf("%lld %lld\n",(a*a-1)/2,(a*a+1)/2);
	}
	else
printf("%lld %lld\n",(a*a-4)/4,(a*a+4)/4);
}
    return 0;  
} 

  不过还是以后可以输出输入尽量都用scanf和printf。。。

 

转载于:https://www.cnblogs.com/Yum20/p/9616375.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值