题解:P10415 [蓝桥杯 2023 国 A] 切割
hostangelb · · 题解
基本思路:
枚举w和h的最小公因数i,最后输出w/i+h/i
但是直接暴力绝对超时
这个时候我们可以针对两个地方进行优化
- 最小公因数绝对不会超过最大公因数,因此我们就可以将i的范围缩小到2~__gcd(w, h)
- 如果__gcd(w, h)等于1,则不用循环,答案直接等于0
AC code
#include <bits/stdc++.h>
using namespace std;
#define int long long
int ans;
signed main()
{
int w, h;
cin >> w >> h;
int x = __gcd(w, h);
if (x != 1)
{
for (int i = 2; i * i <= x; ++i)
{
if (w % i == 0 && h % i == 0)
{
ans = w / i * h / i;
break;
}
}
if (!ans) ans = w / x * h / x;
}
cout << ans << endl;
return 0;
}
AC记录
管理大大求过QWQ
不知廉耻地要赞