1. 程式人生 > >51Nod 1179 最大的最大公約數

51Nod 1179 最大的最大公約數

51nod pla font size clas input open log 輸出

1179 最大的最大公約數

給出N個正整數,找出N個數兩兩之間最大公約數的最大值。例如:N = 4,4個數為:9 15 25 16,兩兩之間最大公約數的最大值是15同25的最大公約數5。 Input
第1行:一個數N,表示輸入正整數的數量。(2 <= N <= 50000)
第2 - N + 1行:每行1個數,對應輸入的正整數.(1 <= S[i] <= 1000000)
Output
輸出兩兩之間最大公約數的最大值。
Input示例
4
9
15
25
16
Output示例
5

n^2的做法顯然不行
看數據範圍 S[i]<=1000000 所以我們可以枚舉每一個數的所有因子 這是可以開下的
然後找從大到小 找出現過兩次以上的數 記為ans

技術分享
 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cctype>
 4 
 5 const int MAXN=1000010;
 6 
 7 int n;
 8 
 9 int cnt[MAXN];
10 
11 inline void read(int&x) {
12     int f=1;register char c=getchar();
13 for(x=0;!isdigit(c);c==-&&(f=-1),c=getchar()); 14 for(;isdigit(c);x=x*10+c-48,c=getchar()); 15 x=x*f; 16 } 17 18 int hh() { 19 read(n); 20 for(int x,i=1;i<=n;++i) { 21 read(x); 22 for(int j=1;j<=sqrt(x);++j) if(x%j==0) ++cnt[j],++cnt[x/j];
23 } 24 for(int i=1000000;i;--i) 25 if(cnt[i]>=2) { 26 printf("%d\n",i); 27 break; 28 } 29 return 0; 30 } 31 32 int sb=hh(); 33 int main(int argc,char**argv) {;}
代碼

51Nod 1179 最大的最大公約數