[CF364D] Ghd

题目描述

John Doe offered his sister Jane Doe find the gcd of some set of numbers a .

Gcd is a positive integer g , such that all number from the set are evenly divisible by g and there isn't such g (g>g) , that all numbers of the set are evenly divisible by g .

Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers.

Ghd is a positive integer g , such that at least half of numbers from the set are evenly divisible by g and there isn't such g (g>g) that at least half of the numbers from the set are evenly divisible by g .

Jane coped with the task for two hours. Please try it, too.

1<=n<=1061<=ai<=1012

随机一个数,他有 12 的概率在所选的集合中。考虑随机 10 个 ai,现在要判断一个数的所有因数是否都是符合要求的。

我们容易在 O(n) 的时间内求出答案,但是还不够。注意到 ai1012,质因子个数至多为 1000。对于每个 j 求出 gcd(ai,j)=d,然后枚举任意两个因数,统计每种因数的出现次数即可。

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
mt19937 gen(time(0));
typedef long long LL;
LL read()
{
	LL s=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
		ch=getchar();
	while(ch>='0'&&ch<='9')
		s=s*10+ch-48,ch=getchar();
	return s;
}
LL ans=0,a[N],pr[N];
int n,k,s[N];
LL gcd(LL x,LL y)
{
	if(!y)
		return x;
	return gcd(y,x%y);
}
mt19937_64 gen1(time(0));
int main()
{
	n=read();
	for(int i=1;i<=n;i++)
		a[i]=read();
	for(int T=1;T<=10;T++)
	{
		int x=gen()%n+1,k=0;
		for(int i=1;1LL*i*i<=a[x];i++)
		{
			if(a[x]%i==0)
			{
				if(1LL*i*i^a[x])
					pr[++k]=i,s[k]=0;
				pr[++k]=a[x]/i,s[k]=0;
			}
		}
		sort(pr+1,pr+k+1);
		for(int i=1;i<=n;i++)
			s[lower_bound(pr+1,pr+k+1,gcd(a[i],a[x]))-pr]++;
		for(int i=1;i<=k;i++)
			for(int j=1;j<i;j++)
				if(pr[i]%pr[j]==0)
					s[j]+=s[i];
		for(int i=1;i<=k;i++)
			if(s[i]>=(n+1>>1))
				ans=max(ans,pr[i]);
	}
	printf("%lld",ans);
	return 0;
}
posted @   灰鲭鲨  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示