zoukankan      html  css  js  c++  java
  • [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/

    Time limit(ms): 1000        Memory limit(kb): 65535
     
    Description
    哈特13最近在学习数论问题,然后他智商太低,并学不懂。这不,他又碰到不会的题了。题意非常简单:

    有n个数字,求出这些数字中两两最大公约数的最大值。你一定要帮助他解决这个问题啊。

    Input

    多组输入,约25组,直到文件末尾。
    每组数据占2行,第一行为数字个数n,2<=n<=100000
    第二行即为对应的n个数,a1到an,1<=ai<=100000

     
    Output

    输出问题的答案,每组输出占一行。

    Sample Input

     
    4
    1 2 3 4

    Sample Output

    2


     
    Sorry,由于OJ原因,换行请用
     
     
    解题思路:同样的一个打表问题,把所有数据全部处理了,利用vector容器把所有的数的因子全部存贮,统计所有输入数字中的每一个因子的覆盖次数
         由于是找最大的gcd,从输入数字的上限(100000)倒着查找,如果某一个因子被覆盖了两次及以上,那么这个因子就是答案~~~
     
    代码如下:
     1 #include<iostream>
     2 #include<vector>
     3 #include<cstring>
     4 using namespace std;
     5 #define max 100000
     6 vector<int> mpt[max];
     7 void init(){
     8     for (int i = 1; i <= max; i++)
     9     for (int j = 1; j*i <= max; j++)
    10         mpt[i*j].push_back(i);
    11 }
    12 int main(){
    13     int dp[max], i, cnt, x, n;
    14     init();
    15     while (cin >> n){
    16         memset(dp, 0, sizeof(dp));
    17         while (n--){
    18             cin >> x;
    19             cnt = mpt[x].size();
    20             for (i = 0; i < cnt; i++)
    21                 dp[mpt[x][i]]++;
    22         }
    23         for (i = max; i; i--){
    24             if (dp[i]>1)
    25                 break;
    26         }
    27         cout << i << "
    ";
    28     }
    29     return 0;
    30 }
    View Code
         
  • 相关阅读:
    Poj 2017 Speed Limit(水题)
    Poj 1316 Self Numbers(水题)
    Poj 1017 Packets(贪心策略)
    Poj 1017 Packets(贪心策略)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2000 Gold Coins(水题)
    poj 2000 Gold Coins(水题)
  • 原文地址:https://www.cnblogs.com/zyxStar/p/4582240.html
Copyright © 2011-2022 走看看