zoukankan      html  css  js  c++  java
  • 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('
    ')
    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
     
  • 相关阅读:
    多台计算机之间数据同步——1.[转]网线制作图解教程
    离心泵的使用注意事项泄露或未排气造成扬程不够
    家庭上网用路由器和ADSL的连接
    专业FLV地址解析
    [求助]带程序访问控制的防火墙 eTrust Personal Firewall 和卡巴斯基2009引起冲突造成系统频繁死机
    DV录像带导出一定要用1394
    Cursor:url()的使用
    理解并解决JavaScript内存泄漏
    CodeIgniter的HMVC
    关于在IE下JavaScript的 Stack overflow at line 错误可能的原因
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/11221736.html
Copyright © 2011-2022 走看看