zoukankan      html  css  js  c++  java
  • BC水题--The Factor(质因分解)

    网址:http://acm.hdu.edu.cn/showproblem.php?pid=5428

    roblem Description

    There is a sequence of n positive integers. Fancycoder is addicted to learn their product, but this product may be extremely huge! However, it is lucky that FancyCoder only needs to find out one factor of this huge product: the smallest factor that contains more than 2 factors(including itself; i.e. 4 has 3 factors so that it is a qualified factor). You need to find it out and print it. As we know, there may be none of such factors; in this occasion, please print -1 instead.
     

    Input
    The first line contains one integer T (1T15), which represents the number of testcases.

    For each testcase, there are two lines:

    1. The first line contains one integer denoting the value of n (1n100).

    2. The second line contains n integers a1,,an (1a1,,an2×109), which denote these n positive integers.
     

    Output
    Print T answers in T lines.
     

    Sample Input
    2 3 1 2 3 5 6 6 6 6 6
     

    Sample Output
    6 4
     

    Source
     

    Recommend
    hujie   |   We have carefully selected several similar problems for you:  5431 5430 5429 5427 5426

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    #include <string>
    #include <queue>
    #include <string.h>
    #include <map>
    #include <set>
    #include <vector>
    #include <algorithm>
    #include <stdlib.h>
    using namespace std;
    #define eps 1e-8
    #define INF 200000005
    #define rd(x) scanf("%d",&x)
    #define rdLL(x) scanf("%I64d",&x)
    #define rd2(x,y) scanf("%d%d",&x,&y)
    #define ll long long
    #define mod 998244353
    #define maxn 100005
    #define maxm 1000
    #define minn 0.00000001;
    ll minfac1=INF,minfac2=INF;
    
    #define N 100010
    bool isprm[N];
    vector <int> vec;
    void isprime()
    {
        int i,j;
        int s,e=sqrt( double(N) )+1;    	//sqrt是对于double数开平方
    
        memset(isprm,1,sizeof(isprm));
                                                      //prm[k++]=2;
        isprm[0] = isprm[1] = 0;
        for(i=4 ;i < N; i=2+i)
    		isprm[i]=0;
    
        for(i=3;i<e;i=2+i)
            if(isprm[i])
                for(s=i*2,j=i*i;j<N;j=j+s)
                    isprm[j]=0;                        //因为j是奇数,所以+奇数后是偶数,不必处理
    
        for(int i=2 ; i<N ; i++)
          if(isprm[i])
            vec.push_back(i);
    }
    
    void getfac(int n)
    {
        for(int i=0 ; i < vec.size() ; i++)
        {
            int x=vec[i];
            if( (x >= minfac1 && x >= minfac2) || n < x )  break;  ///节省时间,去掉可以
    
            while(n%x==0){
             n=n/x;
             if(x<minfac1) minfac1=x;
             else if(x<minfac2) minfac2=x;
            }
        }
        
        if( n!=1 && (minfac1>n||minfac2>n) )  ///可能最后还剩下一个质数   千万不能忘
           minfac1>minfac2 ? minfac1 = n : minfac2 = n;
    }
    
    int main ()
    {
        isprime();
        int Case,temp;
        rd(Case);
        while(Case--)
        {
            int n;
            rd(n);
            for(int i=0 ; i < n ; i++)
            {
                rd(temp);
                getfac(temp);
            }
            printf("%lld
    ", ((minfac1==INF||minfac2==INF) ? -1 : minfac1*minfac2) );
            minfac1 = minfac2 = INF;
        }
        return 0;
    }
    





  • 相关阅读:
    Mbs Framework 简介
    回应老赵: 适合C# Actor的消息执行方式 中看也中用的解决方案
    Mini 容器学习笔记5—— 组件的获取
    JS控制文本框只能输入N个字符.
    【转】外挂编写原理
    【转】集合小节
    CSS样式的filter(滤镜效果)
    系统变量(%SystemRoot% ,%windir% ,%temp%,%system%)的表示方法
    客户端调用服务器控件
    Flash MX 认证考试(样题)
  • 原文地址:https://www.cnblogs.com/zswbky/p/5431923.html
Copyright © 2011-2022 走看看