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

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-07-30 06:49:55

SPOJ AMR11E - Distinct Primes 10232【素数打表】的相关文章

UVA1213 Sum of Different Primes(素数打表+dp)

UVA - 1213 Sum of Different Primes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two

hdu 5104 Primes Problem (素数 打表 水)

http://acm.hdu.edu.cn/showproblem.php?pid=5104 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int isp[10000+100]; int prime[10000]; int coun; bool isprime(int x)

spoj 10232 Distinct Primes(打表)

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

SPOJ Distinct Primes 打表

题目链接:http://www.spoj.com/problems/AMR11E/ 题目大意:Lucky Number指的是有至少三个不同素数相乘得到数.问1000以内的素因子. 解题思路:可以发现1000以内的满足条件的数字非常多,因此直接筛选打表,查看每个数的不同素因子个数,如果超过三个就满足条件. 代码: 1 const int maxn = 1e4 + 5; 2 int vis[maxn]; 3 vector<int> primes, lu; 4 5 void dowork(){ 6

SPOJ 10232. 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 Numbe

hdu 2161 Primes 素数打表

在kuangbin带你飞专题看到的,水了一发,但是wa了一次,T了一次,竟然连素数打表都快不会写了. 而且连求素数时候只需到根号n就可以都忘了,假设有因子m大于√n,那么n/m一定小于√n,所以它在√n前面已经被选出来了. 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<vector>

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

Fermat’s Chirstmas Theorem (素数打表的)

Fermat’s Chirstmas Theorem Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice SDUTOJ 2093 Description In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne

Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)

题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质数 ... 我真是蠢啊 还有  vis要用bool类型的!!!!  int会直接爆 代码如下: #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #includ