์‹ค๋ฒ„4 : ์ˆ˜ํ•™ ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

์†Œ์ˆ˜ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํžˆ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

Code

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAX 10000
using namespace std;
int N, M;
bool isPrime[MAX+1];
 
void SeiveofEratosThenes(){
    fill(isPrime+2, isPrime+MAX+1, true);
    for (int i = 2; i*i <= M; i++) {
        if (isPrime[i] == false) continue;
        for (int j = i*i; j <= M; j+=i) {
            isPrime[j] = false;
        }
    }
}
 
int main(){
    cin >> N >> M;
    SeiveofEratosThenes();
 
    vector<int> primeArr; int sum = 0;
    for (int i = N; i <= M; i++) {
        if (isPrime[i]) {
            primeArr.push_back(i);
            sum += i;
        }
    }
    if (primeArr.size() == 0) {
        cout << -1 << '\n';
    } else cout << sum << '\n' << primeArr[0] << '\n';
    return 0;
}

Reference