AMR11E - 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 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
题意:让我们找一个数,这个数的因数有三个以上是不同的质数,并找出从小到大1~1000个这样的数,并且输入一个n序号就能对应输出第n个(从小到大)的这个数。
分析:怎么做呢?我们可以用分解质因数来做,并且分解出来的质因数不同。由于一个数的质数一定比它小,而且循环的时候,我们是先找小的,我们可以利用一直除的方法,就能让循环时一定时一个质数。
话不多说,看代码(前面都是构造一个这样的序列,后面就是直接输入输出就行了):
#include<iostream>
using namespace std;
int ans[10005]={0,30,42};
int K=3;
void judge(int k)
{
int T=k;
int t=0;
for(int i=2;i<=k;i++)
{
if(k%i==0) t++;
while(k%i==0)//分解质因数的关键步骤
k/=i;
if(t>=3)
{
ans[K]=T;
K++;
break;
}
}
}
int main()
{
for(int i=43;i<=10000;i++)//我们已经知道42是第二个了,所以我们直接从43开始看。
{
judge(i);
}
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
cout<<ans[n]<<endl;
}
return 0;
}