hdu6441(本原勾股数)

题目:

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.

题意:

给你两个数,n是指数,a是底数,求b,c满足 a^n+b^n=c^n。
实际上就是费马大定理,在n>2时,没有整数成立,=2就是勾股数,这里考察了构造勾股数,
本来用公式的话:

a为大于1的奇数2n+1,b = 2n^2+2n,c = b+1;
a为大于4的偶数2n时,b = n^2-1,c = n^2+1;

这样就可以构造勾股数了,但是,这道题就T了。。。。
所以,后来通过构造本原勾股数,预处理,才通过了。
预处理代码:

void ppt(){
    ll s,t;
    for(s = 3; s <= 40000; s += 2){ 
        for(t = 1; t < s; t += 2){
            if(__gcd(s,t) == 1) {
                ll a = s*t; 
                if(a > 40000)
                    break;
                ll b = (s*s-t*t)/2; 
                ll c = (s*s+t*t)/2;
                A[a][0] = b;
                A[a][1] = c;
                if(b > 40000)
                    break; 
                A[b][0] = a;
                A[b][1] = c;
            }
        }
    }
}

代码:

#include<iostream>
using namespace std;

typedef long long ll;
int main()
{
    int t;
    ll n,m;
    while(cin >> t){
cin >> n >> m;
        if(n > 2)
            cout << -1 << " " << -1 << endl;
        else{
            if(n == 0){
                cout << -1 << " " << -1 << endl;
            }
            if(n == 1)
            {
                cout << 1 <<" " << m+1 << endl;
            }
            if(n == 2){
                if(m <= 2){
                    cout << -1 << " " << -1 << endl;
                }
                else{
                    int x = 0;
                    if(m % 2){
                        x = (m-1)/2;
                        cout << 2*x*x+2*x << " " << 2*x*x+2*x+1 << endl;
                    }
                    else{
                        x = m/2;
                        cout << x*x-1 << " " << x*x+1 << endl;
                    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值