codeforces #Round 368 div2-C(勾股数)

来源:互联网 发布:软件渠道代理 编辑:程序博客网 时间:2024/06/05 16:57

题目链接:http://codeforces.com/contest/707/problem/C

题意:给出一个数,输出两个数,这三个数能构成勾股数。

思路:先观察平方数1,4,9,16,25,36,49.......

显然n==1(无法构成三角形)或n==2(三边2,3,4不能构成直角三角形)无解。

数列相邻两项之差( n + 1 ) ^ 2 - n ^ 2 =  2n + 1为奇数,故:

当输入a为奇数时,令√(2n+1) = x     =>    b = n = (x ^ 2 - 1) / 2,c = n + 1 = ( x ^ 2 + 1 ) / 2 ;

数列每隔两项的差( n + 2 ) ^ 2 - n ^ 2 = 4 ( n + 1 )为偶数,故:

当输入a为偶数时,令2√(n+1) = x      =>      b = n = (x ^ 2 - 4) / 4  , c = n + 2 =  (x ^ 2 + 4) / 4。

#include<bits/stdc++.h>using namespace std;typedef long long ll;int main(){    ll n;    scanf("%I64d",&n);    if(n<3)    {        printf("-1\n");        return 0;    }    if(n&1)    {        printf("%I64d %I64d\n",(n * n - 1)>>1,(n * n + 1) >> 1);        return 0;    }    printf("%I64d %I64d\n",(n * n - 4) >> 2,(n * n + 4) >> 2);    return 0;}


0 0
原创粉丝点击