【题解】codeforces1029F[Codeforces Round #506 (Div. 3)]F.Multicolored Markers set

题目链接

Description

There is an infinite board of square tiles. Initially all tiles are white.

Vova has a red marker and a blue marker. Red marker can color a a tiles. Blue marker can color b b tiles. If some tile isn’t white then you can’t use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly a a red tiles and exactly b b blue tiles across the board.

Vova wants to color such a set of tiles that:

they would form a rectangle, consisting of exactly a + b a+b colored tiles;
all tiles of at least one color would also form a rectangle.
Here are some examples of correct colorings:
在这里插入图片描述

Here are some examples of incorrect colorings:

在这里插入图片描述
Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain?

It is guaranteed that there exists at least one correct coloring.

Input

A single line contains two integers a a and b b ( 1 a , b 1 0 14 ) (1≤a,b≤10^{14}) — the number of tiles red marker should color and the number of tiles blue marker should color, respectively.

Output

Print a single integer — the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly a a tiles red and exactly b b tiles blue.

It is guaranteed that there exists at least one correct coloring.

Examples

Input

4 4

Output

12

Input

3 9

Output

14

Input

9 3

Output

14

Input

3 6

Output

12

Input

506 2708

Output

3218

Note

The first four examples correspond to the first picture of the statement.

Note that for there exist multiple correct colorings for all of the examples.

In the first example you can also make a rectangle with sides 1 1 and 8 8 , though its perimeter will be 18 18 which is greater than 8 8 .

In the second example you can make the same resulting rectangle with sides 3 3 and 44$, but red tiles will form the rectangle with sides 1 1 and 3 3 and blue tiles will form the rectangle with sides 3 3 and 3 3 .


参考了大佬题解
显然边长必须得整除面积(废话),能够想到把能整除 a a b b 的边长都存上,然后碰到能整除 a + b a+b 的边长就查找是否存在。但是这样错了,参考了大佬的题解后想明白了,不一定要刚好存在,长度可以包含最小的能整除 a a b b 的边长就可以了。

#include<cstdio>
#include<set>
using namespace std;
typedef long long ll;
ll a,b,ans=1e18;
set<ll>s;
int main()
{
    scanf("%lld%lld",&a,&b);
    for(ll len=1;len*len<=a+b;len++)
    {
    	if(a%len==0)s.insert(a/len);
    	if(b%len==0)s.insert(b/len);
		if((a+b)%len==0&&*s.begin()<=(a+b)/len)ans=min(ans,len+(a+b)/len);
	}
	printf("%lld\n",ans*2);
	return 0;
}

总结

审题不仔细,写的时候没有想到可以不是刚好整除 a a b b 而是包含能整除 a a b b 的最小边长。

猜你喜欢

转载自blog.csdn.net/qq_41958841/article/details/82832517