Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 구현
- 그래프
- 유니온 파인드
- 스택
- 다익스트라
- VR
- 정렬
- 누적 합
- 트리
- 유니티
- 자료구조
- BFS
- ue5
- 문자열
- 다이나믹 프로그래밍
- Team Fortress 2
- 알고리즘
- 백트래킹
- DFS
- 백준
- c++
- 수학
- 투 포인터
- 재귀
- XR Interaction Toolkit
- Unreal Engine 5
- 우선순위 큐
- 브루트포스
- 그리디 알고리즘
- 시뮬레이션
Archives
- Today
- Total
1일1알
백준 1644번 소수의 연속합 C++ 본문
에라토스테네스의 체를 이용해서 소수만 들어가있는 배열을 만들고 그 배열에서 투 포인터를 이용해서 누적합이 n보다 작으면 오른쪽 포인터를 증가시키고 반대면 왼쪽 포인터를 증가시키면서 누적합이 n과 같은 경우의 수들을 찾았다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n;
vector<bool> isPrime;
vector<int> primes;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
isPrime = vector<bool>(n + 1, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i <= sqrt(n); i++) {
if (!isPrime[i])
continue;
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
for (int i = 2; i <= n; i++) {
if (!isPrime[i])
continue;
primes.push_back(i);
}
if (primes.size() == 0) {
cout << 0;
return 0;
}
int sum = primes[0];
int left = 0;
int right = 0;
int cnt = 0;
while (right < primes.size()) {
if (sum == n) {
cnt++;
sum -= primes[left];
left++;
right++;
if (right < primes.size())
sum += primes[right];
}
else if (sum < n) {
right++;
if (right < primes.size())
sum += primes[right];
}
else {
sum -= primes[left];
left++;
}
}
cout << cnt;
}
'알고리즘' 카테고리의 다른 글
백준 2342번 Dance Dance Revolution C++ (0) | 2022.07.21 |
---|---|
백준 2143번 두 배열의 합 C++ (0) | 2022.07.20 |
Judge - 1231 : 양궁 선수 순위 예측 C++ (0) | 2022.07.18 |
Judge - 1233 : 한기대 최고의 보안 시스템 C++ (0) | 2022.07.17 |
Judge - 1236 : 넌 얼마나 빠르게 왔었니? (0) | 2022.07.16 |