Find Terrorists(素數篩選+素因子分解)

御史大夫發表於2012-09-02

 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。利用素因子分解的方法就能統計合數的因子個數。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define dg(i) cout << "*" << i << endl;
using namespace std;

bool prime[2000000] = {1, 1};

//素數篩選
void init_Prime()
{
    int i, j;
    for(i = 2; i < 1000000; i++)
    {
        if(!prime[i])
        {
            for(j = 2; j * i < 2000000; j++)
            {
                prime[i * j] = 1;
            }
        }
    }
}

//素因子分解
bool Count(int x)
{
    if(x == 1) return false;
    if(!prime[x]) return true;
    int cnt = 1, e, i;
    vector<int> v;
    vector<int>::iterator it;
    for(i = 2; i <= x / 2; i++)
    {
        if(!prime[i] && x % i == 0)
        {
            e = 0;
            int tx = x;
            do
            {
                e++;
                tx /= i;
            }while(tx % i == 0);
            v.push_back(e);
        }
    }
    if(!v.empty())
    {
        for(it = v.begin(); it != v.end(); it++)
        {
            cnt *= (*it + 1);
        }
    }
    if(prime[cnt]) return false;
    else return true;
}

int main()
{
    vector<int> num;
    vector<int>::iterator it;
    int T, L, H, i;
    init_Prime();
    scanf("%d", &T);
    while(T--)
    {
        num.clear();
        scanf("%d %d", &L, &H);
        for(i = L; i <= H; i++)
        {
            if(Count(i))
            {
                num.push_back(i);
            }
        }
        if(num.empty()) printf("-1\n");
        else
        {
            it = num.begin();
            printf("%d", *it);
            for(it++; it != num.end(); it++)
                printf(" %d", *it);
            printf("\n");
        }
    }
    return 0;
}


相關文章