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
     
  • 相关阅读:
    Java的字节流,字符流和缓冲流对比探究
    团队作业4:第七篇Scrum冲刺博客(歪瑞古德小队)
    团队作业4:第六篇Scrum冲刺博客(歪瑞古德小队)
    团队作业4:第五篇Scrum冲刺博客(歪瑞古德小队)
    团队作业4:第四篇Scrum冲刺博客(歪瑞古德小队)
    EACCES: permission denied,mkdir … npm install 安装依赖问题解决
    100% 展示 MySQL 语句执行的神器-Optimizer Trace
    关于冲击响应的通俗理解
    struct中的对齐准则
    NIO实践-HTTP交互实现暨简版Tomcat交互内核
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/11221736.html
Copyright © 2011-2022 走看看