SPOJ 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
 

题意:找第n个由至少三个不同素因子组成的数。

思路:n<=1000直接暴力打表预处理

/** @Date    : 2016-12-11-19.01
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version :
  */
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;


int pri[N];
int ans[10010];
int c = 0;
bool vis[N];
int prime()
{
    MMF(vis);
    for(int i = 2; i < N; i++)
    {
        if(!vis[i])
            pri[c++] = i;

        for(int j = 0; j < c && pri[j]*i < N; j++)
        {
            vis[i * pri[j]] = 1;
            if(i % pri[j] == 0)
                break;
        }
    }
}


int main()
{
    prime();
    int tot = 0;
    for(int i = 0; i <= 10000; i++)
    {
        int t = i;
        int cnt = 0;
        for(int j = 0; j < c && pri[j]*pri[j] <= t; j++)
        {
            if(t % pri[j] == 0)
            {
                cnt++;
                while(t % pri[j] == 0)
                    t /= pri[j];
            }
        }
        if(t > 1)
            cnt++;
        if(cnt >= 3)
            ans[tot++] = i;
    }
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        printf("%d\n", ans[n - 1]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/Yumesenya/p/6163255.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值