Triangle CodeForces - 407A
原题链接
考察:枚举
思路:
预处理平方,分斜边和直角边,两个直角边两种情况处理.
Code
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
const int N = 1010;
int pows[N],a,b;
vector<PII> v[N];
bool solve()
{
if(a>b) swap(a,b);
if(!v[a].size()) return 0;
for(int i=0;i<v[a].size();i++)
{
int x = v[a][i].first,y = v[a][i].second;
for(int j=0;j<v[b].size();j++)
{
int c = v[b][j].first,d = v[b][j].second;
if(d-y<=0) continue;
if((x+c)*(x+c)+(d-y)*(d-y)==a*a+b*b)
{
puts("YES");
printf("1 1\n%d %d\n%d %d\n",1+x,1+y,1+x+c,1+y-d);
return 1;
}
}
}
int x = b*b-a*a;
int d = sqrt(x);
if(d*d!=x) return 0;
b = d;
if(a>b) swap(a,b);
for(int i=0;i<v[a].size();i++)
{
int x = v[a][i].first,y = v[a][i].second;
for(int j=0;j<v[b].size();j++)
{
int c = v[b][j].first,d = v[b][j].second;
if(d-y<=0) continue;
if((x+c)*(x+c)+(d-y)*(d-y)==a*a+b*b)
{
puts("YES");
printf("1 1\n%d %d\n%d %d\n",1+x,1+y,1+x+c,1+d-y);
return 1;
}
}
}
return 0;
}
int main()
{
scanf("%d%d",&a,&b);
for(int i=1;i<N;i++)
{
pows[i] = i*i;
for(int j=1;j<i;j++)
{
int x = pows[i]-pows[j];
int d = sqrt(x);
if(d*d!=x) continue;
v[i].push_back({d,j});
}
}
if(!solve()) puts("NO");
return 0;
}