codeforces364D

Ghd

 CodeForces - 364D 

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.

Input

The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier.

Output

Print a single integer g — the Ghd of set a.

Examples

Input
6
6 2 3 4 5 6
Output
3
Input
5
5 5 6 10 15
Output
5

题意:n个数中取一半,使得gcd最大

sol:似乎是鬼畜的随机化算法,每次随机取一个,然后计算所有数字与它的gcd,再随便判断一下个数是否满足一半就好了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll s=0; bool f=0; char ch=' ';
    while(!isdigit(ch))    {f|=(ch=='-'); ch=getchar();}
    while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0) {putchar('-'); x=-x;}
    if(x<10) {putchar(x+'0'); return;}
    write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=1000005;
int n,m;
ll a[N],cnt=0,b[N],ans=0;
struct Node{ll num; int cnt;}c[N];
inline ll gcd(ll a,ll b)
{
    return (!b)?(a):(gcd(b,a%b));
}
inline void Solve()
{
    ll tmp=a[rand()%n+1];
//    cout<<"tmp="<<tmp<<endl;
    int i,j;
    for(i=1;i<=n;i++) b[i]=gcd(a[i],tmp);
    sort(b+1,b+n+1);
    cnt=0; c[++cnt].num=b[1]; c[cnt].cnt=1;
    for(i=2;i<=n;i++)
    {
        if(b[i]!=b[i-1]){c[++cnt].num=b[i]; c[cnt].cnt=0;} c[cnt].cnt++;
    }
    for(i=1;i<=cnt;i++)
    {
        int sum=0;
        for(j=1;j<=cnt;j++) if(c[j].num%c[i].num==0) sum+=c[j].cnt;
        if(sum>=m) ans=max(ans,c[i].num);
    }
}
int main()
{
    srand(20030310);
    int i;
    R(n);
    for(i=1;i<=n;i++) R(a[i]);
    m=(n+1)/2;
    for(i=1;i<=10;i++) Solve();
    Wl(ans);
    return 0;
}
/*
input
6
6 2 3 4 5 6
output
3

input
5
5 5 6 10 15
output
5
*/
View Code

 

 

转载于:https://www.cnblogs.com/gaojunonly1/p/11221736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值