Find Terrorists(素数筛选+素因子分解)

在总理和他的内阁寻求解决恐怖主义问题之际,他们接到了来自上帝的暗示:找到那些数字,这些数字的因数个数为质数。这些数字似乎隐藏了恐怖分子的位置。本文将探讨如何通过编程帮助寻找满足条件的数字。

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

 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值