[CF364D] Ghd
题目描述
John Doe offered his sister Jane Doe find the gcd of some set of numbers .
Gcd is a positive integer , such that all number from the set are evenly divisible by and there isn't such , that all numbers of the set are evenly divisible by .
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 , such that at least half of numbers from the set are evenly divisible by and there isn't such that at least half of the numbers from the set are evenly divisible by .
Jane coped with the task for two hours. Please try it, too.
随机一个数,他有 的概率在所选的集合中。考虑随机 10 个 ,现在要判断一个数的所有因数是否都是符合要求的。
我们容易在 的时间内求出答案,但是还不够。注意到 ,质因子个数至多为 1000。对于每个 求出 ,然后枚举任意两个因数,统计每种因数的出现次数即可。
#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App