【codeforces 757B】 Bash's Big Day

time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu’s Lab. Since Bash is Professor Zulu’s favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases.

But Zulu warns him that a group of k > 1 Pokemon with strengths {s1, s2, s3, …, sk} tend to fight among each other if gcd(s1, s2, s3, …, sk) = 1 (see notes for gcd definition).

Bash, being smart, does not want his Pokemon to fight among each other. However, he also wants to maximize the number of Pokemon he takes from the lab. Can you help Bash find out the maximum number of Pokemon he can take?

Note: A Pokemon cannot fight with itself.

Input
The input consists of two lines.

The first line contains an integer n (1 ≤ n ≤ 105), the number of Pokemon in the lab.

The next line contains n space separated integers, where the i-th of them denotes si (1 ≤ si ≤ 105), the strength of the i-th Pokemon.

Output
Print single integer — the maximum number of Pokemons Bash can take.

Examples
input
3
2 3 4
output
2
input
5
2 3 4 6 7
output
3
Note
gcd (greatest common divisor) of positive integers set {a1, a2, …, an} is the maximum positive integer that divides all the integers {a1, a2, …, an}.

In the first sample, we can take Pokemons with strengths {2, 4} since gcd(2, 4) = 2.

In the second sample, we can take Pokemons with strengths {2, 4, 6}, and there is no larger group with gcd ≠ 1.

【题目链接】:http://codeforces.com/contest/757/problem/B

【题解】

枚举那个最大的组的gcd是什么(枚举那个gcd的质因子就好);
所以找出1..10W里面的所有质数S;
然后枚举以S的倍数为gcd的组的大小;
取最大值就好;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int MAXN = 1e5+100;

vector <int> v;
int n;
int a[MAXN];
int bo[MAXN];

bool is(int x)
{
    int ma = sqrt(x);
    rep1(i,2,ma)
        if (x%i==0)
            return false;
    return true;
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    rep1(i,2,100000)
        if (is(i))
            v.pb(i);
    rei(n);
    rep1(i,1,n)
    {
        rei(a[i]);
        bo[a[i]]++;
    }
    int len = v.size();
    int ans = 1;
    rep1(i,0,len-1)
    {
        int num = 0,x = v[i];
        for (int now = x;now<=100000;now+=x)
            num+=bo[now];
        ans = max(ans,num);
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626719.html