博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Find Terrorists(素数筛选+素因子分解)
阅读量:7117 次
发布时间:2019-06-28

本文共 2729 字,大约阅读时间需要 9 分钟。

 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

31 11 22 5

Sample Output

-122 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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #define LL long long 9 #define MAXI 214748364710 #define MAXL 922337203685477580711 #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
v;39 vector
::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 do47 {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
num;68 vector
::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 else85 {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 }

 

 

转载地址:http://yhyel.baihongyu.com/

你可能感兴趣的文章
Laravel之路(事务)mysql事务
查看>>
Aurora的安装和中文配置
查看>>
oracle数据库出现“批处理中出现错误: ORA-00001: 违反唯一约束条件”解决方法
查看>>
SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute
查看>>
Java多线程:死锁
查看>>
【深度学习系列】CNN模型的可视化
查看>>
memory consistency
查看>>
CSS选择器的新用法
查看>>
PowerShell 并行执行任务
查看>>
C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
查看>>
VC++ Splash Window封装类CSplash
查看>>
wcf配置参数说明
查看>>
假期小结 BIO, NIO, AIO
查看>>
小知识温习重点摘要
查看>>
Comparable和Comparator的区别
查看>>
WEB服务器搭建–IIS
查看>>
Chromium Settings页面修改
查看>>
如何用Python计算Softmax?
查看>>
给你的app添加桌面widget
查看>>
h5移动端混编总结
查看>>