一道数论好题(math)

一道数论好题(math)

Time Limit:1000ms   Memory Limit:128MB

题目描述

rsy最近在研究欧几里得算法,不会的同学可以去看下课件以及代码……

现在她想到了一个新的问题,求k个数的最大公约数!

事实上这个问题仍然很简单。所以rsy想强化一下,她觉得最大公约数等于1就不是很有趣了。因此她想在n个数中找最多的数,使得它们的最大公约数不等于1。

现在rsy想知道,能找最多多少个数。

输入格式(math.in)

    第一行一个数n。

    第二行n个数ai。

输出格式(math.out)

    一个数表示答案。

输入样例

5

2 3 4 5 6

输出样例

3

数据范围

对于30%的数据n<=5。

对于50%的数据n<=20。

对于80%的数据n<=1000,ai<=1000。

对于100%的数据1<=n<=100000,1<=ai<=100000,数据几乎是随机的。

对于这道题的爆0,我也非常无语,只有一个‘=’的问题,0分与90分,就是一个等号的区别,感觉好背,说一下错了哪里,看下面代码第25行

&&10行我定义的数组少了1,所以就死去了,不然就90分了,当然如果是随机数字,那么如果只找2的倍数,应该就能拿到不少分;看我打表;


 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<string>
 7 
 8 using namespace std;
 9 const int N=100001;
10 const int chu[21]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,49,53,59,61,67};
11 
12 int a[N];
13 
14 int main()
15 {
16     freopen("math.in","r",stdin);
17     freopen("math.out","w",stdout);
18     int n;
19     scanf("%d",&n);
20     for(int i=1;i<=n;i++)
21     {
22         scanf("%d",&a[i]);
23     }
24     int ans,maxn=-1;
25     for(int i=1;i<=21;i++)
26     {
27         ans=0;
28         for(int j=1;j<=n;j++)
29         {
30             if(chu[i]<=a[j])
31             {
32                 if(a[j]%chu[i]==0)
33                  {
34                    ans++;
35                  }
36             }
37             
38         }
39         if(ans>maxn)
40         {
41             maxn=ans;
42         }
43     }
44     printf("%d",maxn);
45     fclose(stdin);
46     fclose(stdout);
47 }


 

ac正解代码

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 int n,A,a[1000005],now,MAX,i,j;
 8 int main()
 9 {
10     freopen("math.in","r",stdin);
11     freopen("math.out","w",stdout);
12     scanf("%d",&n);
13     for (i=1; i<=n; i++)
14     {
15         scanf("%d",&A);
16         a[A]++;
17     }
18     for (i=2; i<=1000000; i++)
19     {
20         now=0;
21         for (j=i; j<=1000000; j+=i) now+=a[j];
22         MAX=max(MAX,now);
23     }
24     cout<<MAX;
25     return 0;
26 }

这道题也很水嘛,对于我的爆0,我也感觉非常不幸,也需经验问题吧

原文地址:https://www.cnblogs.com/lyqlyq/p/6804002.html