【题解】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 a tiles. Blue marker can color b 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 a red tiles and exactly b 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 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 a and b b b ( 1 ≤ a , b ≤ 1 0 14 ) (1≤a,b≤10^{14}) (1a,b1014) — 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 a tiles red and exactly b 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 1 and 8 8 8, though its perimeter will be 18 18 18 which is greater than 8 8 8.

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


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

### Codeforces Round 1013 Div. 3 部分题目解析 #### A. Good Numbers 此题的核心在于统计输入数据中特定数字的频率并验证其是否满足条件。具体实现方法如下: 程序通过 `map` 数据结构存储每个数字出现的次数,并逐一检查这些计数值是否达到指定的要求。 ```cpp void solve() { int n, m, k; cin >> n; map<int, int> mp; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { mp[a[i]]++; if (mp[2] >= 2 && mp[0] >= 3 && mp[1] >= 1 && mp[3] >= 1 && mp[5] >= 1) { cout << i << endl; return; } } cout << 0 << endl; } ``` 上述代码逻辑清晰地展示了如何利用哈希表来解决该问题[^1]。 --- #### B. Team Training 对于本题,目标是从给定字符串中删除某些字符以形成子串 `"2020"`。解决方案采用枚举的方式尝试不同的分割位置组合,从而快速判断是否存在合法方案。 以下是完整的 C++ 实现代码片段: ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define endl &#39;\n&#39; void solve() { int n; string a; cin >> n >> a; for (int i = 0; i <= 4; i++) { if (a.substr(0, i) + a.substr(n - (4 - i), 4 - i) == "2020") { puts("YES"); return; } } puts("NO"); } signed main() { int _ = 1; cin >> _; while (_--) solve(); } ``` 这段代码实现了对所有可能情况的有效覆盖,并提供了高效的解答方式。 --- #### 更多题目概述 虽然未提供其他题目的详细描述,但可以根据比赛惯例推测其余部分涉及的内容范围广泛,涵盖了动态规划、贪心算法以及图论等领域知识点。有兴趣者可访问官方平台获取完整版题解资料[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值