首页 > 其他分享 >[CF364D] Ghd

[CF364D] Ghd

时间:2023-08-06 23:34:13浏览次数:41  
标签:set divisible Ghd CF364D ch numbers such evenly

题目描述

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<=10^{6} 1<=a_{i}<=10^{12} $

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

我们容易在 \(O(n)\) 的时间内求出答案,但是还不够。注意到 \(a_i\le 10^{12}\),质因子个数至多为 1000。对于每个 \(j\) 求出 \(\gcd(a_i,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;
}

标签:set,divisible,Ghd,CF364D,ch,numbers,such,evenly
From: https://www.cnblogs.com/mekoszc/p/17610343.html

相关文章

  • 【题解】CF364D
    题目大意给定集合a,求最大的是大小超过一半的子集的最大公约数的数是什么。题解“超过一半”即想到随机化n次后只有\(\frac{1}{2^n}\)的几率错误,于是随机一个数判断它的......
  • 通用串行异步收发器8251的VerilogHDL源代码
     /*****************************************************************************通用串行异步收发器8251的VerilogHDL源代码***************************************......