ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 신기한 소수
    백준알고리즘 2019. 9. 21. 02:38

    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
    46
    47
    #include<iostream>
     
    using namespace std;
     
    int n;
    int prime[] = { 2,3,5,7 };
    bool primee(int x) {
        for (int i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                //cout << "HELLO" << endl;
                return false;
            }
        }
        return true;
    }
    void go(int m, int cnt) {
        if (m == n) {
            cout << cnt << '\n';
            return;
        }
        else {
            if (m == 0) {
                for (int i = 0; i < 4; i++)
                    go(1, prime[i]);
            }
            else {
                cnt *= 10;
                for (int i = 1; i <= 9; i++) {
                    int c = cnt;
                    c += i;
                    if (primee(c))
                        go(m + 1, c);
                }
     
            }
        }
    }
     
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
     
        cin >> n;
        
        go(0,0);
        return 0;
    }
    http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
     

    오랜만에 푸는 소수문제다

    보통 에라토스 테네스의 체를 써서 푸는방법을 많이 써먹는데

    이문제에서 에라토스 테네스의 체를쓰면 메모리 초과에 걸린다 (4MB 제한이다)

    그리고 문제를 보면서 중요한 포인트는 1부터~9까지 모든숫자를 하나하나 다건드릴게아니라 

    시작지점이 2,3,5,7을 최고자리수로 시작해서 하나씩 늘려가면서 소수검사만해보면되고 이또한 생각보다 많지않기떄문에 충분히 4MB와 시간제한을 만족하는 풀이를할수있다.

    '백준알고리즘' 카테고리의 다른 글

    회전하는 큐  (0) 2019.09.21
    촌수계산  (0) 2019.09.21
    사다리 조작  (0) 2019.09.21
    게리맨더링  (0) 2019.09.20
    상범 빌딩  (0) 2019.09.17

    댓글

Designed by Tistory.