Codeforces Round #391 -B. Bash's Big Day

来源:互联网 发布:firewall cmd 80端口 编辑:程序博客网 时间:2024/05/22 04:34

记录一个菜逼的成长。。

题目链接
题目大意:
给你n个数,问最大的gcd不为1的集合大小。

把gcd都变成素因子。
先筛选素数,统计包含素因子的数的个数。
有几个剪枝:
1.如果是1直接跳过
2.如果是素数直接加1
然后就直接枚举素因子并统计

#include <bits/stdc++.h>using namespace std;#define cl(a,b) memset(a,b,sizeof(a))typedef long long LL;const int maxn = 100000 + 10;int a[maxn];int isnotprime[maxn];int cnt[maxn];void init(){    for( LL i = 2; i < maxn; i++ ){        if(!isnotprime[i]){            for( LL j = i*i; j < maxn; j += i ){                isnotprime[j] = 1;            }        }    }}int main(){    //fin,fout;    init();    int n;    while(~scanf("%d",&n)){        for( int i = 0; i < n; i++ ){            scanf("%d",a+i);            if(a[i] == 1)continue;            if(!isnotprime[a[i]]){                cnt[a[i]]++;                continue;            }            int k = sqrt(a[i]);            for( int j = 2; j <= k; j++ ){                if(a[i] % j == 0){                    if(!isnotprime[j])cnt[j]++;                    int tmp = a[i] / j;                    if(a[i] != j*j && !isnotprime[tmp])cnt[tmp]++;                }            }        }        int ans = 0;        for( int i = 0; i < maxn; i++ ){            ans = max(ans,cnt[i]);        }        printf("%d\n",max(ans,1));    }    return 0;}

下面这个应该是标程
看了运行时间,我的程序的效率较高。

#include <bits/stdc++.h>using namespace std;const int N = 1234567;int cnt[N];int main() {  int n;  scanf("%d", &n);  for (int i = 0; i < n; i++) {    int foo;    scanf("%d", &foo);    cnt[foo]++;  }  int ans = 1;  for (int i = 2; i < N; i++) {    int cur = 0;    for (int j = i; j < N; j += i) {      cur += cnt[j];    }    ans = max(ans, cur);  }  printf("%d\n", ans);  return 0;}
0 0