素数筛选

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=43142#problem/B

Description

While preparing this problem set the jury has run into the following problem: it was necessary to send by e-mail the texts of the problems. As it is well known, e-mail is not reliable, messages are sent not enciphered, there is a danger that someone can intercept them. The members of the program committee wanted no participant know the texts of the problems before the start of the contest. That's why they resorted to cryptography methods in order to save the texts of the problems from an unsanctioned reading. The jury gas worked up a new way of enciphering of a text. It is not patented yet, so it's kept secret. However, we'll reveal you one secret: the new algorithm is based on the work with prime numbers. In particular, in uses a calculation of  n-th by order prime number.
Several members of the program committee independently have worked up programs that make such calculations, but these programs produce different answers. Each one of the programmers is sure that his program works correctly. That's why the jury has reached the deadlock and can't continue working. The contest is about not to take place.
You are to help to the jury and to save the contest. We want you to write a program that calculates the  n-th by order prime number. The main thing is that your program should work correctly.

Input

First line contains a positive integer  k. Then  k positive integers follow (one in each line). The numbers don't exceed 15000.

Output

For each number  n you should output the  n-th by order prime number. Each number should be in its line.

Sample Input

input output
4
3
2
5
7
5
3
11
17

Sample Output

5
3
11
17

Hint

The  prime number is a positive integer that has exactly two different positive divisors, i.e. 1 is not a prime number.
素数筛选,老经典了。。。。。

#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
const int N=165005;
bool prime[N];
long long p[N];
int main()
{
    long long t,n,i,j,k=0;
    memset(prime,true,sizeof(prime));
    for(i=2; i<N; i++)
    {
        if(prime[i])
        {
            p[k++]=i;
            for(j=i*i; j<=N; j+=i)
                prime[j]=false;
        }
    }
    while(~scanf("%lld",&t))
    {
      while(t--)
      {
          scanf("%lld",&n);
          printf("%lld\n",p[n-1]);
      }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值