[题解]P4571 [JSOI2009] 瓶子和燃料

思路

首先考虑两个瓶子容量分别为 \(x,y\) 互相倒的情况。发现每一次都是将剩余的液体减少 \(x\) 或者 \(y\),因此这两个瓶子的贡献就是 \(ax + by\),其中 \(a,b\) 是常数。

根据裴蜀定理,\(\min\{ax + by\} = \gcd(x,y)\)。拓展到多个数,就是使得选取的 \(k\) 个数的 \(\gcd\) 最大。

Code

#include <bits/stdc++.h>
#define re register
#define int long long

using namespace std;

const int N = 1010;
int n,k,ans;
unordered_map<int,int> vis;
vector<int> p;

inline int read(){
	int r = 0,w = 1;
	char c = getchar();
	while (c < '0' || c > '9'){
		if (c == '-') w = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9'){
		r = (r << 3) + (r << 1) + (c ^ 48);
		c = getchar();
	}
	return r * w;
}

signed main(){
	n = read(),k = read();
	for (re int i = 1;i <= n;i++){
		int x;
		x = read();
		for (re int j = 1;j * j <= x;j++){
			if (x % j == 0){
				p.push_back(j),vis[j]++;
				if (j != x / j) p.push_back(x / j),vis[x / j]++;
			}
		}
	}
	for (auto x:p){
		if (vis[x] >= k) ans = max(ans,x);
	}
	printf("%lld",ans);
	return 0;
}
posted @ 2024-06-26 12:37  WBIKPS  阅读(17)  评论(0)    收藏  举报