HDU2710(素数打表)

来源:互联网 发布:第五届淘宝村高峰论坛 编辑:程序博客网 时间:2024/05/29 05:01

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710


解题思路:

打出素数表,找出每个数的最大素因子,取最大的即可。


完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 20001;int a[maxn] , b[maxn];int pri[maxn];int cnt;bool ispri[maxn];void dopri(){    memset(ispri , true , sizeof(ispri));    cnt = 0;    ispri[0] = false;    ispri[1] = false;    for(int i = 2 ; i < maxn ; i ++)    {        if(ispri[i])        {            pri[cnt++] = i;            for(int j = i * i ; j < maxn ; j += i)                ispri[j] = false;        }    }}void in(int n){    memset(a , 0 , sizeof(a));    for(int i = 0 ; i < n ; i ++)        cin >> a[i];}int solve(int n){    in(n);    memset(b ,  0 , sizeof(b));    for(int i = 0 ; i < n ; i ++)    {        int maxx = -INF;        for(int j = 0 ; pri[j] <= a[i] ; j ++)        {            if(a[i] % pri[j] == 0 && pri[j] > maxx)            {                maxx = pri[j];            }        }        b[i] = maxx;    }    int key = 0 , maxx = -INF;    for(int i = 0 ; i < n ; i ++)    {        //cout << b[i] << " " ;        if(b[i] > maxx)        {            maxx = b[i];            key = i;        }    }    //cout << endl;    return a[key];}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    std::ios::sync_with_stdio(false);    std::cin.tie(0);    int n;    dopri();    while(cin >> n)    {        cout << solve(n) << endl;    }}


0 0