ACM/ICPC 2011 Asia-Amritapuri Site / E Distinct Primes(求数的素因子)

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 <= 1000
Time Limit: 2 s
Memory Limit: 32 MB

Sample Input:

2
1
2

Sample Output:

30

42

 

分析:

 

简单模拟题。就是注意筛法求素数和怎么求一个数的素因子

 

代码:

 

View Code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdlib>

using namespace std;

#define MAX 50000

int noprime[MAX];

void Prime(int n)
{
    int k;
    noprime[0]=noprime[1]=1;
    noprime[2]=0;
    for (int i=2;i<n;i++)
        if (!noprime[i])
        {
            k=i;
            while(k+i<n)
            {
                k=k+i;
                noprime[k]=1;
            }
        }
}

bool judge(int n)
{
    int num=0;
    for (int i=2;i<40000;i++)//求一个数的素因子
        if (!noprime[i])
        {
            if (n%i==0) num++;
            if (num>=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<<i<<endl;
                            break;
                        }
                    continue;
                }
    }
    return 0;
}

转载于:https://www.cnblogs.com/AbandonZHANG/archive/2012/07/18/2598398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值