SPOJ AMR11E - Distinct Primes 10232【素数打表】

AMR11E - Distinct Primes

no tags 

 

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

 

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;
}


你可能感兴趣的:(SPOJ AMR11E - Distinct Primes 10232【素数打表】)