标签:namespace ica numbers each 超时 while span col 很多
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.
The first line contains the number of test cases T. Each of the next T lines contains one integer n.
Output T lines, containing the corresponding lucky number for that test case.
1 <= T <= 20
1 <= n <= 1000
Sample Input: 2 1 2 Sample Output: 30 42
题意:求第n个三个以上不同的素数相乘的值(从小到大排),但是只要求三个不同的,也就意味着第五个第六个可以是相同的,如果只是暴力打表求三个不同素数的值,那会漏了很多情况,比如第三个应该是2*3*5*2=60.
思路:枚举从30-10000个数,进行分解质因数,如果能分解出三个以上,则将该数字存到另一个数组,一开始想破脑袋都没想出来,后来看了一下数据范围和通过人数,就知道应该是暴力求解,果然没超时!
代码:
1 #include<iostream> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 int a[11000]; 6 int ss(int n)//分解质因数 7 { 8 int q,w,e,c=0; 9 int t=n; 10 for(int i=2;i<=n;i++) 11 { 12 if(n%i==0)//如果i为因数就+1 13 c++; 14 while(n%i==0)//去掉重复的因数 15 { 16 n/=i; 17 } 18 if(n==1) 19 break; 20 } 21 if(c>=3)//如果质因数大于等于3则说明该数字符合条件 22 return 1; 23 else 24 return 0; 25 } 26 int main() 27 { 28 int m=0,c=0; 29 long long ans[11000]; 30 for(int i=30;i<=10000;i++)//应该第一个样例告诉了你开头是30,所以直接从30枚举。 31 { 32 if(ss(i)) 33 a[c++]=i;//保存在数组里,方便直接输出。 34 } 35 int t,n; 36 cin>>t; 37 while(t--) 38 { 39 cin>>n; 40 cout<<a[n-1]<<endl;//因为数从小到大枚举所以直接就可以输出 41 } 42 return 0; 43 }
标签:namespace ica numbers each 超时 while span col 很多
原文地址:https://www.cnblogs.com/spongeb0b/p/9287267.html