跳到正文

题解

题解:P15444 「IXOI R1」出题人完全不会给题目起名字

__slch__约 6 分钟
原文

这是 2026年2月25日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月25日
收录
2026年2月25日
更新
2026年11月12日
篇幅
约 6 分钟
获赞
2
题目
P15444

版本

  1. v2当前
  2. v1

P15444题解

赛事很好想到这个地方最小值肯定是 minmax\frac{min}{max},唯一我当时的纠结点就是这里可不可能要除多次才能得到这个结果?

在我犹豫是否用ST表+贪心时,dalao__luo2031__ 已经 AC。当时就直接打了个除一次的解法上去,对了。当时没细想赛后还是证明出来了。

证明过程

为何选除数和被除数分别为最小最大值

下图是除数和被除数随大小变化的图像(也就是函数 f(x,y)=xyf(x,y)=\frac{x}{y} 的图像):

显然,yy 越靠右越小,xx 越靠前越小。所以如何选择就是顺理成章的了。

为何只用除一次

当我们对整个序列 ÷max{a}\div \text{max}\{a\} 时,我们可以保证所有数都 ≤1\leq 1,故如果再继续操作就不可能带来收益。所以只需要除一次最大值即可。

想到解法后因为我代码固有的大常数和猎奇码风第一发 long long 交上去只有 1 个点。逐花巨量时间改成 __int128 解法 AC。(你可能会说:改 __int128 不是改下输出就行了吗?但是我当时重载的输入输出,故只能使用 std::istream 和 std::ostream 的功能实现。)

接下来奉上我 131 行的猎奇代码:

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;

__int128 gcd(__int128 x, __int128 y) {
	while (y) {
		__int128 t = x%y;
		x = y;
		y = t;
	}
	return x;
}

__int128 Abs(__int128 x) {
	if (x < 0)
		return -x;
	else
		return x;
}

struct num {
	__int128 x, y;
	friend istream& operator>>(istream& is, num& tmp) {
		while (isspace(is.peek())) is.get();
		__int128 fact = 1;
		__int128 abso = 0;
		if (is.peek() == '-')
			fact = -1, is.get();
		char c = 0 ;
		while (isdigit(c = is.get())) {
			abso *= 10;
			abso += (c-'0');
		}
		tmp.x = abso*fact;
		while (isspace(is.peek())) is.get();
		fact = 1;
		abso = 0;
		if (is.peek() == '-')
			fact = -1, is.get();
		c = 0;
		while (isdigit(c = is.get())) {
			abso *= 10;
			abso += (c-'0');
		}
		tmp.y = abso*fact;
		
		return is;
	}
	friend ostream& operator<<(ostream& os, num& tmp) {
		if (tmp.x < 0)
			os << '-';
		__int128 cur = Abs(tmp.x);
		string out = "";
		stringstream ss;
		while (cur > 0) {
			ss << (char)('0' + (cur % 10));
			cur /= 10;
		}
		out = ss.str();
		ss.str("");
		reverse(out.begin(), out.end());
		os << out << ' ';
		if (tmp.y < 0)
			os << '-';
		cur = Abs(tmp.y);
		out = "";
		while (cur > 0) {
			ss << (char)('0' + (cur % 10));
			cur /= 10;
		}
		out = ss.str();
		reverse(out.begin(), out.end());
		os << out << '\n';
		
		return os;
	}
	bool operator>(const num& k) const {
		__int128 nm = (y*k.y)/(gcd(k.y, y));
		__int128 c1 = (nm/y)*x,
			c2 = (nm/k.y)*k.x;
		return c1>c2;
	}
	bool operator<(const num& k) const {
		__int128 nm = (y*k.y)/(gcd(k.y, y));
		__int128 c1 = (nm/y)*x,
		c2 = (nm/k.y)*k.x;
		return c1<c2;
	}
	num operator+(num k) {
		__int128 nm = (y*k.y)/(gcd(k.y, y));
		__int128 c1 = (nm/y)*x,
		c2 = (nm/k.y)*k.x;
		return {c1+c2, nm};
	}
	num operator-(num k) {
		__int128 nm = (y*k.y)/(gcd(k.y, y));
		__int128 c1 = (nm/y)*x,
		c2 = (nm/k.y)*k.x;
		return {c1-c2, nm};
	}
	num operator*(num k) {
		num res = {k.x*x, k.y*y};
		__int128 g = gcd(res.x, res.y);
		res.x /= g;
		res.y /= g;
		return res;
	}
	num operator/(num k) {
		num res = {k.y*x, k.x*y};
		__int128 g = gcd(res.x, res.y);
		res.x /= g;
		res.y /= g;
		return res;
	}
};

int n;
num a[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	num a1 = *(max_element(a+1, a+n+1));
	num a2 = *(min_element(a+1, a+n+1));
	num res = a2/a1;
	cout << res;
	
	return 0;
}