반응형

문제 출처 :


https://www.acmicpc.net/problem/15965



알고리즘 분석 :


문제 해결에 필요한 사항

1. 에라토스테네스의 체


우선 문제에서 최대 50만번째 소수까지 찾으면 된다 했으니 50만번째 소수를 찾아본 결과 750만 이전에서 끝이남을 알 수 있다.


따라서 소수를 구하는 에라토스테네스 체를 이용하여 소수를 구하고 최종적으로 N번째 소수를 출력하면 된다.






소스 코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory.h>
#include <vector>
 
#define MAX_N 7500000
 
using namespace std;
 
int n = MAX_N;
bool isPrime[MAX_N + 1];
 
void eratosthenes()
{
    memset(isPrime, 1sizeof(isPrime));
 
    isPrime[0= isPrime[1= false;
 
    int sqrtn = int(sqrt(n));
 
    for (int i = 2; i <= sqrtn; i++)
        if (isPrime[i])
            for (int j = i*i; j <= n; j += i)
                isPrime[j] = false;
}
 
vector<int> vc;
int main()
{
    eratosthenes();
    
    for (int i = 0; i <= MAX_N; i++)
        if (isPrime[i])
            vc.push_back(i);
 
    int n;
    cin >> n;
    cout << vc[n - 1];
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus

반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1038번] 감소하는 수  (0) 2018.09.25
[16159번] 전광판의 숫자  (0) 2018.09.22
[9882번] Balanced Teams  (0) 2018.09.21
[15927번] 회문은 회문아니야!!  (0) 2018.09.19
[5670번] 휴대폰 자판  (0) 2018.07.15