CodeForces-1029F Multicolored Markers

用a或b形成小矩形,用a+b形成大矩形,小矩形只要能放在大矩形里就可以了

大矩形的长和宽要尽量接近,详细见代码注释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll p[10000000]; //数组尽量开大了,不清楚会有多少个因数
int main()
{
	ll a,b;
	cin>>a>>b;
	//以a作为小矩形
	ll t=sqrt(a);
	int cnt=0;
	ll q=t;
	for(;q>=1;q--)
		if(a%q==0) p[cnt++]=q; //统计因数个数
	sort(p,p+cnt); //对因数排序
	ll k=sqrt(a+b);
	ll x=k,y;
	ll ans=0;
	for(;x>=1;x--) //寻找大矩形的因数
	{
		if((a+b)%x==0)
		{
			y=(a+b)/x; //x,y为大矩形长宽
			int i;
			if(p[cnt-1]<x) i=cnt-1;
			else i=lower_bound(p,p+cnt,x)-p; //查找小矩形能否放在大矩形内
			if(p[i]>x) i--;
			if(i==-1) continue;
			q=a/p[i];
			if(y>=q) //可以就得到了答案
			{
				ans=2*(x+y);
				break;
			}
		}
	}
	//交换a,b
	t=a;
	a=b;
	b=t;
	//重复以上操作
	t=sqrt(a);
	cnt=0;
	q=t;
	for(;q>=1;q--)
		if(a%q==0) p[cnt++]=q;
	sort(p,p+cnt);
	k=sqrt(a+b);
	x=k;
	for(;x>=1;x--)
	{
		if((a+b)%x==0)
		{
			y=(a+b)/x;
			int i;
			if(p[cnt-1]<x) i=cnt-1;
			else i=lower_bound(p,p+cnt,x)-p;
			if(p[i]>x) i--;
			if(i==-1) continue;
			q=a/p[i];
			if(y>=q)
			{
				ans=min(ans,2*(x+y)); //取最小答案
				break;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值