【codeforces 707C】Pythagorean Triples

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

【题意】

给你一个数字n;
问你这个数字是不是某个三角形的一条边;
如果是让你输出另外两条边的大小;

【题解】

首先明确n<=2的时候是无解的。
n>2之后都有解;
这里设
n2+b2=a2
则有
n2=a2b2
也即
n2=(a+b)(ab)
这里对n分两类讨论;

n为奇数
则令
ab=1
a+b=n2
这样
2a=n2+1
因为n是奇数所以右边是个偶数;
得出来的a就是整数了符合题意;
然后b=a-1

n为偶数
则令
ab=2
a+b=n2/2

2a=n2/2+2
a=n2/4+1
因为n是大于2的偶数,所以a最后得到的结果是个整数且大于2;
b=a-2;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;

LL n;
LL sqr(LL x) { return x*x; }

int main(){
    //freopen("F:\rush.txt", "r", stdin);
    rel(n);
    if (n <= 2) return puts("-1"), 0;
    if (n & 1)
    {
        LL b = (sqr(n) + 1) / 2;
        LL a = b - 1;
        printf("%lld %lld
", a, b);
    }
    else
    {
        LL b = sqr(n) / 4 + 1;
        LL a = b - 2;
        printf("%lld %lld
", a, b);
    }
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626480.html