欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-08 11:06:21
...

D - Find Integer----------------------------思维(2018ccpc+数论+费马大定理+奇偶数列法则)
题意:
给定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= (2n+1)b= (n2+(n+1)21)c= (n2+(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.

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

具体证明: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;
}