[JSOI2009]瓶子和燃料 BZOJ2257 数学

题目描述

jyy就一直想着尽快回地球,可惜他飞船的燃料不够了。有一天他又去向火星人要燃料,这次火星人答应了,要jyy用飞船上的瓶子来换。jyy的飞船上共有 N个瓶子(1<=N<=1000) ,经过协商,火星人只要其中的K 个。

jyy将 K个瓶子交给火星人之后,火星人用它们装一些燃料给 jyy。所有的瓶子都没有刻度,只在瓶口标注了容量,第i个瓶子的容量为Vi(Vi 为整数,并且满足1<=Vi<=1000000000 ) 。火星人比较吝啬,他们并不会把所有的瓶子都装满燃料。他们拿到瓶子后,会跑到燃料库里鼓捣一通,弄出一小点燃料来交差。jyy当然知道他们会来这一手,于是事先了解了火星人鼓捣的具体内容。

火星人在燃料库里只会做如下的3种操作:

  1. 将某个瓶子装满燃料;
  2. 将某个瓶子中的燃料全部倒回燃料库;
  3. 将燃料从瓶子a倒向瓶子b,直到瓶子b满或者瓶子a空。燃料倾倒过程中的损耗可以忽略。

火星人拿出的燃料,当然是这些操作能得到的最小正体积。jyy知道,对于不同的瓶子组合,火星人可能会被迫给出不同体积的燃料。jyy希望找到最优的瓶子组合,使得火星人给出尽量多的燃料。

输入输出格式

输入格式:

第1行:2个整数N,K。
第2..N 行:每行1个整数,第i+1 行的整数为Vi

输出格式:

仅1行,一个整数,表示火星人给出燃料的最大值。

输入输出样例

输入样例#1: 复制
3 2
3
4
4
输出样例#1: 复制
4

说明

选择第2 个瓶子和第 个瓶子,火星人被迫会给出4 体积的容量。

讲道理,和我出的趣味赛题一样好吧。。

就两个瓶子为例,最小的自然就是每次不停地相互倒油,最后只剩下gcd(a,b);

那么简单来说就是选K个使得gcd最大!

那就是一道简单题了。。。

当然不能用之前遍历可能因子的方法了,因为 n<=1e9;

那么我们在统计的时候维护一下最大值就ok了;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n, k;
int p[maxn];
map<int, int>Map;

int main()
{
	//ios::sync_with_stdio(0);
	rdint(n); rdint(k);
	int maxx = -inf;
	int Maxx = -inf;
	for (int i = 1; i <= n; i++) {
		int x; rdint(x); maxx = max(maxx, x);
		for (int j = 1; j <= sqrt(x); j++) {
			if (x%j == 0) {
				Map[j]++;
				if (j*j != x)Map[x / j]++;
				if (Map[j] == k)Maxx = max(Maxx, j);
				if (Map[x / j] == k)Maxx = max(Maxx, x / j);
			}
		}
	}
	int cnt = 0;
	cout << Maxx << endl;
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/zxyqzy/p/10134612.html