Pythagorean Triples CodeForces - 707C
原题链接
考察:思维
思路:
\[a^2+b^2=c^2
\]
\[a^2 = c^2-b^2 = (c+b)(c-b)
\]
\[令c+b = a^2 ,c-b = 1则
\]
\[c = \frac{a^2+1}{2} , b = \frac{a^2-1}{2}
\]
此时分5种情况:
(1) n = 1,无解
(2) n = 2,无解
(3) n为奇数,此时计算输出
(4) n为偶数,不断/2直到获得奇数.
(5) n只有2的因子,对应 3 4 5的情况.
Code
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
int n;
void solve()
{
if(n==1||n==2) {puts("-1");return;}
if(n&1)
{
LL a = (LL)n*n+1>>1;
LL b = (LL)n*n-1>>1;
printf("%lld %lld\n",b,a);
return;
}
int cnt = 0;
while(n%2==0)
{
n/=2;
cnt++;
}
if(n==1)
{
cnt-=2;
printf("%lld %lld\n",3*(1ll<<cnt),5*(1ll<<cnt));
return;
}
LL a = ((LL)n*n+1>>1)*(1ll<<cnt);
LL b = ((LL)n*n-1>>1)*(1ll<<cnt);
printf("%lld %lld\n",b,a);
}
int main()
{
scanf("%d",&n);
solve();
return 0;
}