spoj 10232 Distinct Primes(打表)

来源:互联网 发布:域名记录类型 编辑:程序博客网 时间:2024/06/05 22:38
 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;} 


1 0
原创粉丝点击