Codeforces Round #239 (Div. 2)

C. 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的直角三角形,问是否在平面坐标系中存在这个的三角形,并且它的三个坐标顶点坐标都是整数,任意一条边都不与坐标轴平行或垂直,如果存在这样的三角形,输出其坐标顶点,否则输出-1.
思路:首先将直角顶点坐标定为(0,0),然后枚举另外两个坐标的横坐标,枚举范围分别为(0,a),(-b,0)或者(-a,0),(0,b)。然后加限制条件剪枝判断。
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <map>
 6 #include <algorithm>
 7 using namespace std;
 8 int main()
 9 {
10     int a,b;
11     while(cin>>a>>b)
12     {
13         int x2,y2,x3,y3;
14         int flag = 0;
15         for (int i = 1; i <= a; i++)
16         {
17             double m = sqrt(a*a-i*i);
18             y2 = (int)m;
19             if(m!=y2)
20                 continue;
21             for (int j = -b; j < 0; j++)
22             {
23                 m = sqrt(b*b-j*j);
24                 y3 = (int)m;
25                 if(m!=y3)
26                     continue;
27                 if(y2==y3)
28                     continue;
29                 if((i-j)*(i-j)+(y3-y2)*(y3-y2)==a*a+b*b)
30                 {
31                     x2 = i;
32                     x3 = j;
33                     flag = 1;
34                     break;
35                 }
36             }
37             if (flag)
38                 break;
39         }
40         if (flag)
41         {
42             printf("YES
");
43             printf("0 0
");
44             printf("%d %d
%d %d
",x2,y2,x3,y3);
45         }
46         else
47             printf("NO
");
48     }
49     return 0;
50 }
View Code
原文地址:https://www.cnblogs.com/lahblogs/p/3635683.html