-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[CJW] 9월 2주차
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Ex_1208 부분수열의 합 2 [골1] | ||
|
||
def getSubset(length, start): | ||
target = [0] * (1 << length) | ||
for i in range(1 << length): | ||
for j in range(length): | ||
if (i & (1 << j)) > 0: | ||
target[i] += nList[start + j] | ||
|
||
return target | ||
|
||
|
||
N, S = map(int, input().split()) | ||
nList = list(map(int, input().split())) | ||
answer = 0 | ||
|
||
a = N // 2 | ||
b = N - a | ||
|
||
left = sorted(getSubset(a, 0)) | ||
right = sorted(getSubset(b, a), reverse=True) | ||
|
||
i, j = 0, 0 | ||
while i < (1 << a) and j < (1 << b): | ||
|
||
if left[i] + right[j] == S: | ||
left_stack, right_stack = 1, 1 | ||
|
||
while i < ((1 << a) - 1) and left[i] == left[i + 1]: | ||
left_stack += 1 | ||
i += 1 | ||
|
||
while j < ((1 << b) - 1) and right[j] == right[j + 1]: | ||
right_stack += 1 | ||
j += 1 | ||
|
||
answer += left_stack * right_stack | ||
i, j = i + 1, j + 1 | ||
|
||
elif left[i] + right[j] < S: | ||
i += 1 | ||
|
||
else: | ||
j += 1 | ||
|
||
print(answer if S != 0 else answer - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Ex_1644 소수의 연속합 [골3] | ||
import math | ||
from collections import defaultdict | ||
|
||
N = int(input()) | ||
|
||
primes = [] | ||
isPrime = [True] * (N + 1) | ||
sumDict = defaultdict(int) | ||
answer = 0 | ||
|
||
# set prime bool | ||
for i in range(2, int(math.sqrt(N)) + 1): | ||
if isPrime[i]: | ||
j = 2 | ||
while i * j <= N: | ||
isPrime[i * j] = False | ||
j += 1 | ||
|
||
# set prime num | ||
for i in range(2, N + 1): | ||
if isPrime[i]: | ||
primes.append(i) | ||
|
||
# prefix sum | ||
for i in range(len(primes)): | ||
sumPrime = primes[i] | ||
|
||
if sumPrime == N: | ||
answer += 1 | ||
|
||
for j in range(i + 1, len(primes)): | ||
sumPrime += primes[j] | ||
|
||
if sumPrime > N: | ||
break | ||
|
||
if sumPrime == N: | ||
answer += 1 | ||
|
||
print(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Ex_16566 카드게임 [플5] | ||
# upper bound | ||
def upperBound(target): | ||
s, e = 0, M | ||
|
||
while s < e: | ||
mid = (s + e) // 2 | ||
if cards[mid] <= target: | ||
s = mid + 1 | ||
else: | ||
e = mid | ||
return e | ||
|
||
|
||
def find(x): | ||
if graph[x] != x: | ||
graph[x] = find(graph[x]) | ||
return graph[x] | ||
|
||
|
||
def union(child, parent): | ||
if parent < M: | ||
graph[find(child)] = find(parent) | ||
|
||
|
||
N, M, K = map(int, input().split()) | ||
cards = sorted(map(int, input().split())) | ||
cCards = list(map(int, input().split())) | ||
graph = list(i for i in range(M)) | ||
|
||
for cCard in cCards: | ||
index = find(upperBound(cCard)) | ||
print(cards[index]) | ||
|
||
union(index, index + 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Ex_2143 두 배열의 합 [골3] | ||
from collections import defaultdict | ||
|
||
T = int(input()) | ||
|
||
n = int(input()) | ||
aList = list(map(int, input().split())) | ||
aSum = defaultdict(int) | ||
|
||
m = int(input()) | ||
bList = list(map(int, input().split())) | ||
|
||
answer = 0 | ||
|
||
for i in range(n): | ||
for j in range(i, n): | ||
aSum[sum(aList[i:j + 1])] += 1 | ||
|
||
for i in range(m): | ||
for j in range(i, m): | ||
answer += aSum[T - sum(bList[i: j + 1])] | ||
|
||
print(answer) |