zoukankan      html  css  js  c++  java
  • The Factor

    The Factor

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

    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
     
    Source

     题意:给一数组。求数列的乘积的最小的满足条件的因子,该因子含有两个以上因子(包括自身。

    解题思路:对数列中的每一个数,找出两个最小素数因子。然后对这些质数因子排序,最小的两个数乘积便是满足条件的因子。

    intention:数比较大,long long。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 
     8 #define N 100000 //N*N > 10的9次方。素数表在N 以内就行。 
     9 
    10 long long b[N], num[N];
    11 int cnt = 0;
    12 
    13 void prim()
    14 {
    15     int a[N] = {0};
    16     for(int i = 2; i < N; i++)
    17     {
    18         if(!a[i])
    19         {
    20             b[cnt++] = i;
    21             for(int j = i+i; j < N; j+=i)
    22                 a[j] = 1;
    23         }
    24     }
    25 }
    26 
    27 int main()
    28 {
    29     int t, n;
    30     long long d;
    31     prim();
    32     scanf("%d", &t);
    33     while(t--)
    34     {
    35         int k = 0;
    36 
    37         scanf("%d", &n);
    38         while(n--)
    39         {
    40             scanf("%I64d", &d);
    41             int q = 0;
    42             for(int i = 0; i < cnt && b[i]<=d && q < 2; i++)   // i<cnt ,如果i> cnt,然而b[i]还是比d小,就会出现除数是0的情况……
    43             {
    44                 while(d % b[i] == 0)
    45                 {
    46                     q++;
    47                     d /= b[i];
    48                     num[k++] = b[i];
    49                     if(q >= 2)
    50                         break;
    51                 }
    52             }
    53 
    54             if(d != 1)
    55                 num[k++] = d;   // 素数表在N范围内,数据范围内的素数超过N,so,剩下的d如果不是1的话,可能是超过N的素数,例如 两个素数乘积2*10016959=20033918.所以10016959要存数组num中
    56 
    57         }
    58         sort(num, num+k);
    59         if(k < 2)
    60             printf("-1
    ");
    61         else
    62             printf("%I64d
    ", num[0]*num[1]);
    63     }
    64     return 0;
    65 }

    思维严谨……

    让未来到来 让过去过去
  • 相关阅读:
    YARN源码学习(七)-----Task级别GC相关指标的自定义counter添加
    YARN源码学习(七)-----Task级别GC相关指标的自定义counter添加
    YARN源码分析(八)-----Reduce Shuffle过程分析
    YARN源码分析(八)-----Reduce Shuffle过程分析
    【每天一道算法题】整数循环节之和——数字黑洞6174
    getline函数
    设计模式之建造者模式Builder(创建型)
    字符串算法总结
    C++设计模式之单例模式
    Linux下C的线程同步机制
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4789042.html
Copyright © 2011-2022 走看看