D - Find Integer----------------------------思维(2018ccpc+数论+费马大定理+奇偶数列法则)

D - Find Integer----------------------------思维(2018ccpc+数论+费马大定理+奇偶数列法则)_第1张图片
题意:
给定n和a, 让你求b和c ,使得 an + bn = cn

解析:
根据费马大定理,an + bn = cn. n>2无解
当n0时 无解
当n
1时 让b=1,c=a+1;
当n==2时 需要用到奇偶数列法则
定理: 如a2+b2=c^2是直角三角形的三个整数边长,则必有如下a值的奇数列、偶数列关系成立;

当a为奇数时
{ a =   ( 2 n + 1 ) b =   ( n 2 + ( n + 1 ) 2 − 1 ) c =   ( n 2 + ( n + 1 ) 2 ) \left\{ \begin{aligned} a & = & \ (2n+1) \\ b & = & \ ( n^2+(n+1)^2-1) \\ c & = & \ (n^2+(n+1)^2) \end{aligned} \right. abc=== (2n+1) (n2+(n+1)21) (n2+(n+1)2)

当a为偶数时
{ a =   ( 2 n ) b =   ( n 2 − 1 ) c =   ( n 2 + 1 ) \left\{ \begin{aligned} a & = & \ (2n) \\ b & = & \ ( n^2-1) \\ c & = & \ (n^2+1) \end{aligned} \right. abc=== (2n) (n21) (n2+1)

具体证明:https://blog.csdn.net/Dilly__dally/article/details/82081922

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll n,a;
int t;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld %lld",&n,&a);
        if(n>2) printf("-1 -1\n");
        else if(n==0) printf("-1 -1\n");
        else if(n==1)
        {
            printf("%lld %lld\n",1,a+1);
        }
        else if(n==2)
        {
            if(a%2){
                ll t=(a-1)/2;
                ll b=t*t+(t+1)*(t+1)-1;
                ll c=t*t+(t+1)*(t+1);
                printf("%lld %lld\n",b,c);
            }
            else
            {
                ll t=a/2;
                ll b=t*t-1;
                ll c=t*t+1;
                printf("%lld %lld\n",b,c);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(思维,数论,费马大定理)