BZOJ2257 [Jsoi2009]瓶子和燃料 【裴蜀定理】

题目链接

BZOJ2257

题解

由裴蜀定理我们知道,若干的瓶子如此倾倒最小能凑出的是其\(gcd\)
现在我们需要求出\(n\)个瓶子中选出\(K\)个使\(gcd\)最大
每个数求出因数排序即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1005,maxm = 1000005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
int n,K;
int p[maxm],pi;
void sp(int x){
	p[++pi] = x;
	for (int i = 2; i * i <= x; i++)
		if (x % i == 0){
			p[++pi] = i;
			if (i * i != x) p[++pi] = x /  i;
		}
}
int main(){
	n = read(); K = read();
	REP(i,n) sp(read());
	sort(p + 1,p + 1 + pi);
	int ans = 1,cnt = 0;
	for (int i = 1; i <= pi; i++){
		if (p[i] != p[i - 1]){
			if (cnt >= K) ans = max(ans,p[i - 1]);
			cnt = 1;
		}
		else cnt++;
	}
	if (cnt >= K) ans = max(ans,p[pi]);
	printf("%d\n",ans);
	return 0;
}

posted @ 2018-05-22 15:24  Mychael  阅读(115)  评论(0编辑  收藏  举报