The Factor HDU - 5428(数论)

The Factor HDU - 5428(分解质因数)

传送门
题目大意:
输入一个序列,求他们所有数的乘积,然后找到最后的这个数的能整除它的最小因子。条件限制,这个最小因子必须满足有两个以上的数能整除它,包括它自己。数据范围 1—2e9

众所周知(口胡开始),一个数能分解为若干个质数相乘(1除外),这道题把序列的每个数分解为最小质因数,用数组存起来,最后排个序,把前两个相乘即可,如果数组长度为一,输出“-1”,即不成立

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include<time.h>
#include <stack>
#include <list>
#include <set>
#include <sstream>
#include <iterator>
using namespace std;
#define FOPI freopen("input.in", "r", stdin)
#define DOPI freopen("output.out", "w", stdout)
#define ll long long int
#define fro(i,a,n) for(ll i=a;i<n;i++)
#define pre(i,a,n) for(ll i=n-1;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define fi first
#define se second
#define s_d(a) scanf("%d",&a)
#define s_lld(a) scanf("%lld",&a)
#define s_s(a) scanf("%s",a)
#define s_ch(a) scanf("%c",&a)
typedef pair<ll,ll> P;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const double PI = 3.1415926535897932;
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
const int maxn = 2e5+100;
int lowbit(int x){return x&(-x);}
vector<ll> s;
void solve(ll n)
{
    ll temp=sqrt(n);
    for(int i=2;i<=temp;i++)
    {
        while(n%i==0)
        {
            n/=i;
            s.push_back(i);
        }
    }
    if(n!=1)
        s.push_back(n);
}
int main()
{
    //ios::sync_with_stdio(0);
    ll t;
    cin>>t;
    while(t--)
    {
        s.clear();
        ll a;
        cin>>a;
        fro(i,0,a)
        {
            ll b;
            cin>>b;
            solve(b);
        }
        sort(s.begin(),s.end());
        if(s.size()<2)
            cout<<-1<<endl;
        else
        {
        ll aa=s[0]*s[1];
        //cout<<1<<endl;
        cout<<aa<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值