2018.8.25CCPC网络赛Find Integer题解(费马大定理+勾股数的求解)

版权声明:转载请注明出处哦~ https://blog.csdn.net/Cassie_zkq/article/details/82118567

题目传送门

Find Integer

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

费马大定理(基础知识):

 费马大定理,又被称为“费马最后的定理”,由17世纪法国数学家皮耶·德·费玛提出。

它断言当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解。

历经三百多年的历史,最终该定理在1995年被英国数学家安德鲁·怀尔斯彻底证明。

另附费马大定理的简单证明过程(来自百度)有兴趣的可以看一下哦

勾股数的两种计算方法:

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

2)

题目解释:

给出a和n,请找到符合a^n+b^n=c^n的b和c,如果存在请输出一组,否则输出-1 -1

解题思路:

对n分n>2, 2,1,0,四种情况讨论

n=2时应用勾股数的计算方法

ac代码:

#include <iostream>
#define ll long long int
using namespace std;
int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        ll n,a;
        scanf("%lld%lld",&n,&a);
        if(n==2)
        {
            ll m=a/2;
            if(a%2==1 && a>1)
            {

                 printf("%lld %lld\n",2*m*m+2*m,2*m*m+2*m+1);
            }
            else if(a%2==0 && a>4)
            {
                printf("%lld %lld\n",m*m-1,m*m+1);
            }
            if(a<3)
                printf("-1 -1\n");
            continue;

        }
        if(n>2||n==0)
        {
            printf("-1 -1\n");
            continue;
        }
        if(n==1)
        {
            printf("%lld %lld\n",a+1,a+a+1);
            continue;
        }

    }
    return 0;
}
//当a为大于1的奇数2n+1时,b=2n^2+2n, c=2n^2+2n+1。
//当a为大于4的偶数2n时,b=n^2-1, c=n^2+1

猜你喜欢

转载自blog.csdn.net/Cassie_zkq/article/details/82118567