SPOJ AMR11E - Distinct Primes 10232【素数打表】
2015-08-20 09:55
274 查看
AMR11E - Distinct Primes
no tagsArithmancy is Draco Malfoy's favorite subject, but what spoils it for him is that Hermione Granger is in his class, and she is better than him at it. Prime numbers are of mystical importance in Arithmancy, and Lucky Numbers even more so. Lucky Numbers are
those positive integers that have at least three distinct prime factors; 30 and 42 are the first two. Malfoy's teacher has given them a positive integer n, and has asked them to find the nth lucky number. Malfoy would like to beat Hermione at this exercise,
so although he is an evil git, please help him, just this once. After all, the know-it-all Hermione does need a lesson.
Input (STDIN):
The first line contains the number of test cases T. Each of the next T lines contains one integer n.
Output (STDOUT):
Output T lines, containing the corresponding lucky number for that test case.
Constraints:
1 <= T <= 20
1 <= n <= 1000
Time Limit: 2 s
Memory Limit: 32 MB
Sample Input:
2
1
2
Sample Output:
30
42
Arithmancy is Draco Malfoy's favorite subject, but what spoils it for him is that Hermione Granger is in his class, and she is better than him at it. Prime numbers are of mystical importance in Arithmancy, and Lucky Numbers even more so. Lucky Numbers are
those positive integers that have at least three distinct prime factors; 30 and 42 are the first two. Malfoy's teacher has given them a positive integer n, and has asked them to find the nth lucky number. Malfoy would like to beat Hermione at this exercise,
so although he is an evil git, please help him, just this once. After all, the know-it-all Hermione does need a lesson.
Input (STDIN):
The first line contains the number of test cases T. Each of the next T lines contains one integer n.
Output (STDOUT):
Output T lines, containing the corresponding lucky number for that test case.
Constraints:
1 <= T <= 20
1 <= n <= 1000
Sample Input:
2
1
2
Sample Output:
30
42
题意:找出第n个lucky数字,lucky数字的定义:最少包含3个素因子,注意是至少,而不是只有3个。。wa在了这里。。。
#include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int MAXN = 10000; int is_prime[MAXN]; int prime[MAXN]; int tot; void find_prime() { tot=0; for(int i=2;i<10000;i++){ if(!is_prime[i]){ prime[tot++]=i; for(int j=i;j<10000;j+=i){ is_prime[j]=1; } } } } int main() { find_prime(); int ans[MAXN]; int t=0; for(int i=30;i<10000;i++){ int temp=i; int cnt=0; for(int j=0;j<tot;j++){ if(temp%prime[j]==0){ cnt++; while(temp%prime[j]==0) temp/=prime[j]; } if(cnt>=3||prime[j]>i) break; } if(cnt>=3) ans[t++]=i; } sort(ans,ans+t); int n; int T; scanf("%d",&T); while(T--){ scanf("%d",&n); printf("%d\n",ans[n-1]); } return 0; }
相关文章推荐
- C#模拟PrtScn实现截屏预备知识--keybd_event使用方法
- 17、SQL Server 备份和还原
- jvm基础知识二
- 分析网站建设销售们在和客户沟通中的录音行为
- Apache与Tomcat整合
- Zabbix快速部署
- 学习迭代器
- LVM常见案例
- 第一次开通博客,以后多多关照!
- iOS 面试常见问题总结2
- Photoshop图层混合(Layer Blending)模式的算法实现
- 修改initrd方法
- ubuntu14.04下配置jdk环境
- 修改initrd方法
- linux 多行操作
- 函数指针的用法
- LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
- awk 用法学习
- 【转】【OpenCV入门教程之一】 安装OpenCV:OpenCV 3.0、OpenCV 2.4.8、OpenCV 2.4.9 +VS 开发环境配置
- [leetcode-145]Binary Tree Postorder Traversal(c++)