跳到正文

题解

题解:P10415 [蓝桥杯 2023 国 A] 切割

hostangelb约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
1
题目
P10415

版本

  1. v1当前

基本思路:

枚举w和h的最小公因数i,最后输出w/i+h/i

但是直接暴力绝对超时

这个时候我们可以针对两个地方进行优化

  1. 最小公因数绝对不会超过最大公因数,因此我们就可以将i的范围缩小到2~__gcd(w, h)
  2. 如果__gcd(w, h)等于1,则不用循环,答案直接等于0

AC code

C++
#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

不知廉耻地要赞

原文