51nod 1179 最大的最大公約數 (無恥的打表計數法)
阿新 • • 發佈:2017-11-28
bsp ret 題目 out log fin clu img https
題目:
考慮清楚就簡單了,我們把每個數的因子計數。
兩個數的公約數就是計數超過2的數,然後找到最大的那個就好了。
計算每個數的素因子,記得sqrt(),不然會超時。
打表計數法時間復雜度O(n*sqrt(n))。
代碼:
#include <iostream> #include <algorithm> #include <map> #include <vector> #include <set> #include <math.h> #include <queue> #include<assert.h> #include <stdio.h> #include <stdlib.h> #include <string> using namespace std; typedef long long ll; #define INF 2147483647 int n; int a[1000010]; int main(){ cin >> n; int key; int ans = 1; while(n--){ cin >> key; int p = sqrt(key);for(int i = 1;i <= p; i++){ if(key%i == 0){ a[i]++; a[key/i]++; // cout << i << endl; } } } for(int i = 2;i <= 1000000; i++){ if(a[i] >= 2){ ans = i; } } cout << ans << endl;return 0; }
51nod 1179 最大的最大公約數 (無恥的打表計數法)