首页 > 其他 > 详细

spoj 10232 Distinct Primes(打表)

时间:2015-08-19 14:52:24      阅读:239      评论:0      收藏:0      [点我收藏+]
 Distinct Primes
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
Time Limit: 2 s
Memory Limit: 32 MB
Sample Input:


2
1
2
Sample Output:


30

42

#include<stdio.h>
#include<string.h>
int p[10000];
int a[10000];
int lucky(int x){
	int num=0,i=2;
	while(x>1){
		if(x%i==0){
			num++;
			while(x%i==0)
				x/=i;
		}else{
			i++;
			while(p[i]){
				i++;
			}
		}
		
	}
	if(num>=3) return 1;
	return 0;
}
void sloved(){
	int i=0;
	int x=30;
	while(i<=1500){
		if(lucky(x)){
			a[++i]=x;
		}
		x++;
	}
}
int main(){
	int i,j;
	memset(p,0,sizeof(p));
	p[0]=p[1]=1; 
	for(int i=2;i*i<10000;i++){
		if(!p[i]){
			for(int j=i*i;j<10000;j+=i){
				p[j]=1;
			} 
		}
	}
	sloved();
	int t,n;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		printf("%d\n",a[n]);
	}
	return 0;
} 


spoj 10232 Distinct Primes(打表)

原文:http://blog.csdn.net/ling_du/article/details/47779877

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!