Fight the Monster CodeForces - 487A

原题链接
考察:枚举+二分
思路:
  枚举其他两个药品的使用量,二分最后的使用量

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;
}
posted @ 2021-07-27 21:20  acmloser  阅读(31)  评论(0编辑  收藏  举报