【暴力】欠扁的 CD

L i n k Link Link

s s l   1495 ssl\ 1495 ssl 1495

D e s c r i p t i o n Description Description

【暴力】欠扁的 CD_第1张图片

S a m p l e Sample Sample I n p u t Input Input

3 1
1	2	3

S a m p l e Sample Sample O u t p u t Output Output

3

H i n t Hint Hint

【暴力】欠扁的 CD_第2张图片

S o l u t i o n Solution Solution

枚举 g c d gcd gcd,然后再枚举 g c d gcd gcd的倍数在原序列中出现的次数,若大于等于k就直接输出即可

C o d e Code Code

#include
#include
#include
#define ll long long

using namespace std;

ll s[500005], a[500005];
ll n, k, maxn;

ll check(int x)
{
	ll sum = 0;
	for (int i = 1; i * x <= maxn; ++i)
		sum += s[i * x];
	if (sum >= k) return 1;
	return 0;
}

int main()
{
	scanf("%lld%lld", &n, &k);
	for (int i = 1; i <= n; ++i){
		scanf("%lld", &a[i]);
		maxn = max(maxn, a[i]);
		s[a[i]]++;//记录每个数出现次数
	} 
	sort(a + 1, a + n + 1);
	for (ll i = a[n]; i >= 1; --i)
	{
		if (check(i)) {
			printf("%lld", i * k);//有k个人,每个人贡献i
			return 0;
		} 
	}
	return 0;
}

你可能感兴趣的:(暴力,暴力)