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。利用素因子分解的方法就能統計合數的因子個數。
#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;
}
相關文章
- HDU44979 GCD and LCM (素因子分解+計數)GC
- HDU 5317 RGCDQ (素因子分解+預處理)GC
- 【數論】素數篩法
- 素數個數 <埃式篩 && 尤拉篩>
- 質數判斷、質因子分解、質數篩
- CodeForces - 463E Caisa and Tree (dfs+素因子分解)AI
- [演算法]: 素數篩法演算法
- HDU 4497GCD and LCM(素數分解)GC
- Diff-prime Pairs(思維+素數篩)AI
- 尤拉素數篩選與命令列傳參啟動C程式命令列C程式
- ACdream 1112 Alice and Bob (博弈&&素數篩選優化)優化
- 面試官本拿求素數搞我,但被我優雅的“回擊“了(素數篩)面試
- 面試官本拿求素數搞我,但被我用素數篩優雅的“回擊“了面試
- 洛谷P3383 【模板】線性篩素數
- 數論線性篩總結 (素數篩,尤拉函式篩,莫比烏斯函式篩,前n個數的約數個數篩)函式
- ACM 素數ACM
- 增補部落格 第二十篇 python 篩法求素數Python
- POJ 2262 Goldbach's Conjecture (求解素數的一般篩和線性篩)Go
- 素數計數函式函式
- 計算素數【Java】Java
- 藍橋杯 演算法訓練 素因子去重(Java)演算法Java
- 裝置畫素、獨立畫素和css畫素CSS
- 表示素數的函式函式
- 密碼工程-大素數密碼
- 如何高效尋找素數
- C語言判斷素數,判斷質素演算法C語言演算法
- 求素數(質數)演算法演算法
- HDU 4542 小明系列故事——未知剩餘系 (DFS 反素數 篩子預處理)
- 初等數論——素數,逆元,EXGCD有關GC
- 1354: 素數判定(C語言)C語言
- c語言 構造素數表C語言
- 計算2..n的素數
- Miller-Rabin素數快速檢測
- 6-2 計算素數和
- 素數判定演算法 初級演算法
- 素數迴文——輸出兩整數之間所有既是迴文數又是素數的數 C++實現C++
- 掌握web開發基礎系列--物理畫素、邏輯畫素、css畫素WebCSS
- PAT1013數素數C++C++