给定一个直角三角形的一边长度。问是否存在一个直角三角形,使得它满足有一边的长度是x

当x=1、2的时候是无解的,可以暴力打表看看。

注意到,相邻的两个数的平方的差值是奇数

x^2 - (x-1)^2 = 2*x-1

间隔为2的两个数的平方的差值是偶数

(x+1)^2 - (x-1)^2 = 4*x

这样就可以分类讨论了。

把n永远当成是指角边就OK

Codeforces Round #368 (Div. 2)   C. Pythagorean Triples  数学_#include

Codeforces Round #368 (Div. 2)   C. Pythagorean Triples  数学_#include_02

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>

void work ()
{
LL n;
cin>>n;
if (n==1 || n==2 )
{
cout<<-1<<endl;
return ;
}
LL t = n*n;
if (t&1)
{
LL k = (n*n+1)>>1;
cout<<k<<" "<<k-1<<endl;
}
else
{
LL k = t>>2;
cout<<k+1<<" "<<k-1<<endl;
}
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return 0;
}

View Code