关闭

Codeforces 407 A. Triangle 【Codeforces Round #239 (Div. 1)】

标签: 暴力枚举
387人阅读 评论(0) 收藏 举报
分类:

点击打开链接

A. Triangle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such a way that none of its sides is parallel to the coordinate axes. All the vertices must have integer coordinates. If there exists such a location, you have to output the appropriate coordinates of vertices.

Input

The first line contains two integers a, b (1 ≤ a, b ≤ 1000), separated by a single space.

Output

In the first line print either "YES" or "NO" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers — the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute value.

Sample test(s)
input
1 1
output
NO
input
5 5
output
YES
2 1
5 5
-2 4
input
5 10
output
YES
-10 4
-2 -2
1 2

题目大意:

给你两个数 a 和 b ,分别是一个直角三角形的两条直角边,让你求出是否存在不与坐标轴平行的三角形的三个坐标。。。

如果存在输出 YES,并且将这三个坐标输出,(任选一个输出),否则输出NO


解题思路:


首先想到的是这两条直角边一定是其他直角三角形的一条斜边。。。然后数据范围并不是很大,所以就直接枚举就行了。

有一个点的坐标是固定的 就是原点 (0, 0)点。。。

上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 3e3+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
    if(b == 0)
        return a;
    return gcd(b, a%b);
}
struct node
{
    int x, y;
};
node p1[maxn], p2[maxn];
int main()
{
    int a, b;
    while(cin>>a>>b)
    {
        int cnt1, cnt2, cnt, j;
        double tmp;
        cnt1 = cnt2 = cnt = 0;
        for(int i=1; i<a; i++)
        {
            tmp = a*a-i*i;
            j = (int)sqrt(tmp);
            if(i*i+j*j == a*a)
            {
                p1[cnt1].x = i;
                p1[cnt1++].y = j;
            }
        }
        for(int i=1; i<b; i++)
        {
            tmp = b*b-i*i;
            j = (int)sqrt(tmp);
            if(i*i+j*j == b*b)
            {
                p2[cnt2].x = i;
                p2[cnt2++].y = j;
            }
        }
        bool ok = false;
        for(int i=0; i<cnt1; i++)
        {
            for(int j=0; j<cnt2; j++)
            {
                if(p1[i].y!=p2[j].y && p1[i].x*p2[j].x==p1[i].y*p2[j].y)
                {
                    puts("YES");
                    puts("0 0");
                    printf("%d %d\n",-p1[i].x,p1[i].y);
                    printf("%d %d\n",p2[j].x,p2[j].y);
                    ok = true;
                    break;
                }
            }
            if(ok)
                break;
        }
        if(!ok)
            puts("NO");
    }
    return 0;
}

 	


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:445336次
    • 积分:10647
    • 等级:
    • 排名:第1610名
    • 原创:632篇
    • 转载:5篇
    • 译文:0篇
    • 评论:89条
    博客专栏