ACM International Collegiate Programming Contest, Asia-Amritapuri Site, 2011
Problem E: 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 <= 1000Time Limit: 2 sMemory Limit: 32 MBSample Input:
2
12Sample Output:
30
42
分析:
简单模拟题。就是注意筛法求素数和怎么求一个数的素因子。
代码:
#include#include #include #include #include using namespace std;#define MAX 50000int noprime[MAX];void Prime(int n){ int k; noprime[0]=noprime[1]=1; noprime[2]=0; for (int i=2;i =3) break; while (n%i==0) n/=i; } if (num>=3) return true; else return false;}int main(){ int t; cin>>t; Prime(MAX); while (t--) { int n; cin>>n; int num=0; int pre=29; for (int i=pre+1;i<=42000;i++) if (judge(i)) { pre=i; num++; if (num==n) { cout< <