CodeForces-707C-勾股定理

题目链接:Pythagorean Triples


题意:给你一个n,问你是否能找到m,k,使得n,m,k是一个直角三角形的三边长。


思路:

假设n是直角边,则

设A = (k + m), B = (k - m),则


因为n,m,k为三角形三边,所以要保证都是整数并且大于0,所以A,B奇偶性应相同,且


所以:

当n = 1 或 n = 2时,无解,

n为奇数时,令, n为偶数时,令


代码:

# include 
# include 
# include 
# include 
# include 
using namespace std;
typedef long long ll;

ll sqr(int x) {
    return (ll)x * x;
}

int main(void)
{
    int n;
    while (~scanf("%d", &n)) {
        if (n == 1 || n == 2) printf("-1\n");
        else {
            if (n % 2 == 1) {
                int B = 1; ll A = sqr(n);
                printf("%lld %lld\n", (A - B) / 2, (A + B) / 2);
            } else {
                int B = 2; ll A = sqr(n) / 2;
                printf("%lld %lld\n", (A - B) / 2, (A + B) / 2);
            }
        }
    }

    return 0;
}


你可能感兴趣的:(数学)