《1649: Max Factor》

求最大质因子,线性筛一下然后就可以暴力了。

或者做质因子分解也可以,因为只求质因子,肯定都可以被分解到。

这里需要明确一个概念,这个质因子包括这个数本身。(这里被wa了)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e4+5;
const int M = 1e6+5;
const LL Mod = 1e9 + 7;
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}
        return x * f;
    }
}
using namespace FASTIO;

bool vis[N];
int prime[N],tot = 0;
void init()
{
    for(int i = 2;i < N;++i)
    {
        if(!vis[i]) prime[++tot] = i;
        for(int j = 1;j <= tot && i * prime[j] < N;++j)
        {
            vis[i * prime[j]] = 1;
            if(i % prime[j] == 0) break;
        }
    }
}
int main()
{
    init();
    int n;
    while(cin >> n)
    {
        int mx = 0,ans = 0;
        for(int i = 1;i <= n;++i)
        {
            int x;x = read();
            int tmp = 0;
            for(int j = 1;j <= tot && prime[j] <= x;++j)
            {
                if(x % prime[j] == 0) tmp = prime[j];
            }
            if(x == 1) tmp = 1;
            if(tmp > mx || i == 1)
            {
                mx = tmp;
                ans = x;
            }
        }
        printf("%d
",ans);
    }
    system("pause");
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zwjzwj/p/14075913.html