Hdu 5428 The Factor【思维】

本文介绍了一个算法问题,目标是在一组正整数的乘积中找出最小的特殊因子——该因子需拥有超过两个因数。文章提供了问题的具体描述、输入输出样例及解决思路,通过分解每个数的因子来确定符合条件的最小因子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2642    Accepted Submission(s): 796


Problem 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

题目大意:


有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.

思路:


1、因为数据范围不大,所以我们暴力拆分每个数的因子,得到一个数组yinzi【i】;


2、对于合法的解,无非也就两种情况:
①这是一个合数因子。

②这是一个由几个素数因子相乘得到的。

③那么显然,两个素数相乘一定是合数,那么没有必要枚举多个素数因子相乘,我们只要维护出来两个最小素数因子记录下来即可。


3、ans=min(两个素数因子相乘,一个最小的合数因子);


4、注意,不要提前break;


Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll __int64
ll a[150];
ll yinzi[150000000];
ll b[5];
ll judge(ll num)
{
    if(num==1||num==0||num==2||num==3)return 0;
    for(int i=2;i<=sqrt(num);i++)
    {
        if(num%i==0)return 1;
    }
    return 0;
}
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        for(ll i=0; i<n; i++)
        {
            cin>>a[i];
        }
        int contz=0;
        for(ll i=0; i<n; i++)
        {
            for(int j=1; j<=sqrt(a[i]); j++)
            {
                if(a[i]%j==0)
                {
                    yinzi[contz++]=j;
                    yinzi[contz++]=a[i]/j;
                }
            }
        }
        sort(yinzi,yinzi+contz);
        int cont=0;
        ll output=4000000000060000000;
        for(int i=0;i<contz;i++)
        {
            if(yinzi[i]<=1)continue;
            if(judge(yinzi[i])==1)
            {
                output=min(output,yinzi[i]);
            }
            else
            {
                if(cont==2)continue;
                b[cont++]=yinzi[i];
            }
        }
        if(cont==2)
        output=min(output,b[0]*b[1]);
        if(output==4000000000060000000)
        {
            cout<<"-1"<<endl;
            continue;
        }
        cout<<output<<endl;
    }
}







内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值