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 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;
} 
时间: 2024-08-29 19:15:24

spoj 10232 Distinct Primes(打表)的相关文章

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

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

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 - DISUBSTR Distinct Substrings (不相同的子串的个数)

Distinct Substrings  Time Limit: 159MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20;Each test case consists of

【SPOJ】Distinct Substrings(后缀自动机)

[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/endpos\)集合的大小 但是实际上我们没有任何必要减去不合法的数量 我们只需要累加每个节点表示的合法子串的数量即可 这个值等于\(longest-shortest+1=longest-parent.longest\) #include<iostream> #include<cstdio&g

解析mysql中:单表distinct、多表group by查询去除重复记录

单表的唯一查询用:distinct多表的唯一查询用:group bydistinct 查询多表时,left join 还有效,全连接无效,在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重复记录的所有值.其原因是distinct只能返回它的目标字段,而无法返回其它字段,用distinct不能解决的话,我只有用二重循环查询来解决,而这样对于一个数据量非常大的

C# DataTable的Distinct解决方案及表的复制

DataTable的Distinct DataTable dataTable; DataView dataView = dataTable.DefaultView; DataTable dataTableDistinct = dataView.ToTable(true,"FieldName1","FieldName2","...");//注:其中ToTable()的第一个参数为是否DISTINCT DataTable 表的复制 DataTable

SPOJ 705 Distinct Substrings(后缀数组)

[题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每个后缀对答案的贡献为n-sa[i]+1-h[i], 因为排名相邻的后缀一定是公共前缀最长的, 那么就可以有效地通过LCP去除重复计算的子串. [代码] #include <cstdio> #include <cstring> #include <algorithm> usi

HDU 2161 Primes(打表)

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9888    Accepted Submission(s): 4136 Problem Description Write a program to read in a list of integers and determine whether or not each n