티스토리 뷰
1644번: 소수의 연속합
첫째 줄에 자연수 N이 주어진다. (1 ≤ N ≤ 4,000,000)
www.acmicpc.net
백준 소스코드 [C++] 1644 소수의 연속합
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
bool notSosu[4000001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 2;i <= sqrt(4000000);i++) {
if (notSosu[i] == false) {
for (int j = i * i;j <= 4000000;j = j +i)
notSosu[j] = true;
}
}
vector<int> prime;
for (int i = 2; i < 4000000; i++) {
if (notSosu[i] == false)
prime.push_back(i);
}
int l = 0, r = 0,sum=0,cnt=0;
while (true) {
if (sum >= n) sum -= prime[l++];
else if (r == prime.size()) break;
else sum += prime[r++];
if (sum == n) cnt++;
}
cout << cnt;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 1799 비숍 (0) | 2021.02.23 |
---|---|
백준 소스코드 [C++] 2283 구간 자르기 (0) | 2021.02.22 |
백준 소스코드 [C++] 11728 배열 합치기 (0) | 2021.02.21 |
백준 소스코드 [C++] 2003 수들의 합 2 (0) | 2021.02.21 |
백준 소스코드 [C++] 14245 XOR (0) | 2021.02.20 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- CPU와 Memory
- 컴퓨터 추상화
- WWDC19
- CompositionalLayout
- 포드 풀커슨 알고리즘
- 최단경로 알고리즘
- 최대 매칭
- 최단경로 문제
- observeOn
- State Restoration
- test coverage
- 에드몬드 카프 알고리즘
- WWDC16
- MeTal
- 네트워크 플로우
- mach-o
- 강한 순환 참조
- WWDC21
- HIG
- 부스트캠프 6기
- 벨만포드 시간복잡도
- rxswift
- 벨만포드 알고리즘
- 네트워크 유량
- 코딩대회
- 다익스트라 시간복잡도
- WWDC17
- 최단경로문제
- Testable
- IOS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함