zoukankan      html  css  js  c++  java
  • Find Terrorists(素数筛选+素因子分解)

     Find Terrorists

    Time limit: 5 seconds


    The Prime Minister and his Accumulated Council of Ministers(ACM) are trying hard to find all possible terrorist locations. In his dream, the Prime Minister gets a message from God suggesting that the answer to all terrorist problems are numbers(say one such number is X) such that the number of factors of X(including 1 and X) is prime. These numbers supposedly contain the encrypted locations of terrorists. Since the ACM has no programmer, the Prime Minister needs your help in finding out such numbers.

    Note:- 1 is not considered a prime number .

    Input

    The first line of input will contain an integer T <= 20 denoting the number of test cases.
    T lines follow, one per test case.
    Each test case will be a line formatted as "L H" where L and H are integers and 0

    Output

    Output one line per case a space separated list of all integers(sorted ascending) lying between L and H(both inclusive) such that the number of factors of each integer is prime.In case no such integer exist output -1.

    Sample Input

    3
    1 1
    1 2
    2 5
    

    Sample Output

    -1
    2
    2 3 4 5
    

    一个素数的因子个数必定是2,2是素数,所以凡是素数都符合条件。对于合数,由于合数能分解为素因子的幂的乘积,比如15 = 3^1 * 5^1;18 = 2^1 * 3^2。分解式中次数加1的乘积就是合数的因子个数,比如15的因子个数是(1 + 1)*(1 + 1) = 4,他的因子是1, 3, 5, 15;18的因子个数是(1 + 1)*(2 + 1) = 6,它的因子是1, 2, 3, 6, 9, 18。利用素因子分解的方法就能统计合数的因子个数。

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <vector>
     8 #define LL long long
     9 #define MAXI 2147483647
    10 #define MAXL 9223372036854775807
    11 #define dg(i) cout << "*" << i << endl;
    12 using namespace std;
    13 
    14 bool prime[2000000] = {1, 1};
    15 
    16 //素数筛选
    17 void init_Prime()
    18 {
    19     int i, j;
    20     for(i = 2; i < 1000000; i++)
    21     {
    22         if(!prime[i])
    23         {
    24             for(j = 2; j * i < 2000000; j++)
    25             {
    26                 prime[i * j] = 1;
    27             }
    28         }
    29     }
    30 }
    31 
    32 //素因子分解
    33 bool Count(int x)
    34 {
    35     if(x == 1) return false;
    36     if(!prime[x]) return true;
    37     int cnt = 1, e, i;
    38     vector<int> v;
    39     vector<int>::iterator it;
    40     for(i = 2; i <= x / 2; i++)
    41     {
    42         if(!prime[i] && x % i == 0)
    43         {
    44             e = 0;
    45             int tx = x;
    46             do
    47             {
    48                 e++;
    49                 tx /= i;
    50             }while(tx % i == 0);
    51             v.push_back(e);
    52         }
    53     }
    54     if(!v.empty())
    55     {
    56         for(it = v.begin(); it != v.end(); it++)
    57         {
    58             cnt *= (*it + 1);
    59         }
    60     }
    61     if(prime[cnt]) return false;
    62     else return true;
    63 }
    64 
    65 int main()
    66 {
    67     vector<int> num;
    68     vector<int>::iterator it;
    69     int T, L, H, i;
    70     init_Prime();
    71     scanf("%d", &T);
    72     while(T--)
    73     {
    74         num.clear();
    75         scanf("%d %d", &L, &H);
    76         for(i = L; i <= H; i++)
    77         {
    78             if(Count(i))
    79             {
    80                 num.push_back(i);
    81             }
    82         }
    83         if(num.empty()) printf("-1\n");
    84         else
    85         {
    86             it = num.begin();
    87             printf("%d", *it);
    88             for(it++; it != num.end(); it++)
    89                 printf(" %d", *it);
    90             printf("\n");
    91         }
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    Leetcode:linked_list_cycle
    关于Go语言共享内存操作的小实例
    程序猿如同妓女
    算法——排序算法个人总结
    CentOS 6.4下安装和配置Samba 第2页_服务器应用_Linux公社-Linux系统门户网站
    解决fedora samba在windows下无权限访问的问题
    基于samba实现win7与linux之间共享文件_阳仔_新浪博客
    增加samba用户提示Failed to add entry for user
    Ubuntu+Win7+Samba实现文件共享_Linux教程_Linux公社-Linux系统门户网站
    Mycat 月分片方法
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910466.html
Copyright © 2011-2022 走看看