bestcoder#54 The Factor

探讨一种算法,用于找出数列中所有数的乘积所含有的最小因子,该因子需拥有超过两个因子。若不存在这样的因子,则返回-1。此算法适用于处理大量数据的情况。

The Factor Accepts: 133 Submissions: 356
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
问题描述
有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.
输入描述
输入文件的第一行有一个正整数T (1≤T≤15),表示数据组数。

接下去有T组数据,每组数据的第一行有一个正整数n (1≤n≤100).

第二行有n个正整数a1….an( 所有数均小于2*10^9) 表示这个数列。
输出描述
输出T行T个数表示每次询问的答案。
输入样例
2
3
1 2 3
5
6 6 6 6 6
输出样例
6
4

说明:
对于每一个数字,它有用的部分其实只有它的所有质因子(包括相等的)。求出所有数的所有质因子中最小的两个,相乘就是答案。如果所有数字的质因子个数不到两个,那么就是无解。时间复杂O(n*sqrt(a))O(n∗sqrt{a})。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> p;

void getFactors(int w)
{
    for(long long i = 2; i*i<=w;i++)
    {
        while( w % i == 0)
        {
            p.push_back(i);
            w /= i;
        }
    }
    if (w>1)
        p.push_back(w);
}
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        p.clear();
        int temp;
        scanf("%d",&n);
        for(int i = 1;i<=n;i++)
        {
            scanf("%d",&temp);
            getFactors(temp);
        }
        sort(p.begin(),p.end());
        if (p.size() < 2)
             printf("-1\n");
        else
           printf("%I64d\n",1LL*p[0]*p[1]);
    }
}
test_cpu_prerun PROC EXPORT test_cpu_prerun ; register R0 test LDR R0, =GENERAL_FACTOR1 CMP R0, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R0, =GENERAL_FACTOR2 CMP R0, #GENERAL_FACTOR2 BNE.W StartupPCFail ; register R14 test MOVS R0, R14 LDR R14, =GENERAL_FACTOR1 CMP R14, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R14, =GENERAL_FACTOR2 CMP R14, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R14, R0 MOVS R0, #0x00 ; register R1 test LDR R1, =GENERAL_FACTOR1 CMP R1, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R1, =GENERAL_FACTOR2 CMP R1, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R1, #0x01 ; register R2 test LDR R2, =GENERAL_FACTOR1 CMP R2, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R2, =GENERAL_FACTOR2 CMP R2, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R2, #0x02 ; register R3 test LDR R3, =GENERAL_FACTOR1 CMP R3, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R3, =GENERAL_FACTOR2 CMP R3, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R3, #0x03 ; register R4 test LDR R4, =GENERAL_FACTOR1 CMP R4, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R4, =GENERAL_FACTOR2 CMP R4, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R4, #0x04 ; register R5 test LDR R5, =GENERAL_FACTOR1 CMP R5, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R5, =GENERAL_FACTOR2 CMP R5, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R5, #0x05 ; register R6 test LDR R6, =GENERAL_FACTOR1 CMP R6, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R6, =GENERAL_FACTOR2 CMP R6, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R6, #0x06 ; register R7 test LDR R7, =GENERAL_FACTOR1 CMP R7, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R7, =GENERAL_FACTOR2 CMP R7, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R7, #0x07 ; register R8 test LDR R8, =GENERAL_FACTOR1 CMP R8, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R8, =GENERAL_FACTOR2 CMP R8, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R8, #0x08 ; register R9 test LDR R9, =GENERAL_FACTOR1 CMP R9, #GENERAL_FACTOR1 BNE.W StartupPCFail LDR R9, =GENERAL_FACTOR2 CMP R9, #GENERAL_FACTOR2 BNE.W StartupPCFail MOVS R9, #0x09 ; register R10 test LDR R10, =GENERAL_FACTOR1 CMP R10, #GENERAL_FACTOR1 BNE StartupPCFail LDR R10, =GENERAL_FACTOR2 CMP R10, #GENERAL_FACTOR2 BNE StartupPCFail MOVS R10, #0x0A ; register R11 test LDR R11, =GENERAL_FACTOR1 CMP R11, #GENERAL_FACTOR1 BNE StartupPCFail LDR R11, =GENERAL_FACTOR2 CMP R11, #GENERAL_FACTOR2 BNE StartupPCFail MOVS R11, #0x0B ; register R12 test LDR R12, =GENERAL_FACTOR1 CMP R12, #GENERAL_FACTOR1 BNE StartupPCFail LDR R12, =GENERAL_FACTOR2 CMP R12, #GENERAL_FACTOR2 BNE StartupPCFail MOVS R12, #0x0C ; verify the values of the registers CMP R0, #0x00 BNE StartupPCFail CMP R1, #0x01 BNE StartupPCFail CMP R2, #0x02 BNE StartupPCFail CMP R3, #0x03 BNE StartupPCFail CMP R4, #0x04 BNE StartupPCFail CMP R5, #0x05 BNE StartupPCFail CMP R6, #0x06 BNE StartupPCFail CMP R7, #0x07 BNE StartupPCFail CMP R8, #0x08 BNE StartupPCFail CMP R9, #0x09 BNE StartupPCFail CMP R10, #0x0A BNE StartupPCFail CMP R11, #0x0B BNE StartupPCFail CMP R12, #0x0C BNE StartupPCFail ; process stack pointer(PSP) test MRS R0, PSP LDR R1, =SP_FACTOR1 MSR PSP, R1 MRS R2, PSP CMP R2, R1 BNE StartupPCFail LDR R1, =SP_FACTOR2 MSR PSP, R1 MRS R2, PSP CMP R2, R1 BNE StartupPCFail MSR PSP, R0 ; stack pointer(SP) test MOV R12, R13 LDR R13, =SP_FACTOR1 LDR R1, =SP_FACTOR1 CMP R13, R1 BNE StartupPCFail LDR R13, =SP_FACTOR2 LDR R1, =SP_FACTOR2 CMP R13, R1 BNE StartupPCFail MOV R13, R12 ; register APSR test MOVS R0, #0x00000000 ; test Z flag BNE.W StartupPCFail SUBS R0,#1 ; test N flag BPL.W StartupPCFail ADDS R0,#2 ; test C flag without setting Z flag BCC.W StartupPCFail MOVS R0, #0x80000000 ADDS R0, R0, R0 ; test V Flag BVC.W StartupPCFail MOVS R0, #0xFFFFFFFF USAT R1,#10,R0 ; test Q Flag MRS R0, APSR ; get the value of APSR register CMP R0, #0xB8000000 BNE.W StartupPCFail MOVS R0, #0x1 ; SUCCESS = 1 BX LR StartupPCFail MOVS R0, #0x00 BX LR StartupPCSuccess MOVS R0, #0x01 BX LR ENDP END
最新发布
09-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值