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     memset(vis, 0, sizeof(vis));
 7     for(int i = 2; i < maxn; i++) if(!vis[i])
 8         for(int j = i * i; j < maxn; j += i)
 9             if(j > 0) vis[j] = 1;
10     for(int i = 2; i < maxn; i++)
11         if(!vis[i]) primes.push_back(i);
12 }
13 void solve(){
14     dowork();
15     memset(vis, 0, sizeof(vis));
16     int cnt = 0;
17     for(int i = 0; i < primes.size(); i++){
18         for(int j = 2; j < maxn; j++){
19             if(vis[j] >= 3) continue;
20             int tmp = 0, x = j;
21             while(x % primes[i] == 0 && x) {
22                 tmp = 1; x /= primes[i];
23             }
24             if(tmp) vis[j]++;
25         }
26     }
27     for(int i = 2; i < maxn; i++)
28         if(vis[i] >= 3) lu.push_back(i);
29 }
30 int main(){
31     ios::sync_with_stdio(false);
32     int t;
33     solve();
34     cin >> t;
35     while(t--){
36         int n;
37         cin >> n;
38         cout << lu[n - 1] << endl;
39     }
40 }

题目:

Distinct Primes

#math #number-theory

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 n-th 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

The first line contains the number of test cases T. Each of the next T lines contains one integer n.

Output

Output T lines, containing the corresponding lucky number for that test case.

Constraints

1 <= T <= 20
1 <= n <= 1000

Example

Sample Input:
2
1
2

Sample Output:
30
42
时间: 2024-08-10 23:30:59

SPOJ Distinct Primes 打表的相关文章

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

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

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

SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

DISUBSTR - Distinct Substrings no tags 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 one string, whose length is <= 1000 Output For each test case output

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

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

SPOJ PGCD - Primes in GCD Table (好题! 莫比乌斯反演+分块求和优化)

PGCD - Primes in GCD Table Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greate

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

HDU 4715 Difference Between Primes (素数表+二分)

Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2998    Accepted Submission(s): 850 Problem Description All you know Goldbach conjecture.That is to say, Every even inte