문제 : https://www.acmicpc.net/problem/1644
[알고리즘풀이]
에라토스테네스 체를 이용해서 소수를 다 구하고, N보다 작은 모든 소수에 대해서 각각 연속합을 구해본다.
그러다 N이랑 같아지면 Count, N보다 커지면 Break 를 한다.
#include<iostream>
using namespace std;
char era[4000001];
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 2; i <= 4000000; i++)
if (era[i] == false)
for (int j = 2 * i; j <= 4000000; j += i)
era[j] = true;
int n, c = 0;
cin >> n;
for (int i = 2; i <= n; i++) {
int temp = 0;
if (era[i] == false) {
for (int j = i; ; j++) {
if (era[j] == false)
temp += j;
if (temp == n) {
c++;
break;
}
if (temp > n) {
break;
}
}
}
}
cout << c;
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 1620 - 나는야 포켓몬 마스터 이다솜 (0) | 2019.09.24 |
---|---|
[BOJ] 1759 - 암호 만들기 (0) | 2019.09.24 |
[BOJ] 2875 - TIMSKO (0) | 2019.09.23 |
[BOJ] 1212 - 8진수 2진수 (0) | 2019.09.23 |
[BOJ] 4355 - Relatives (2) | 2019.09.23 |