Skip to content

Commit

Permalink
Merge pull request #120 from Cha-Ji/solve/#115
Browse files Browse the repository at this point in the history
[CJW] 9월 2주차
  • Loading branch information
Cha-Ji authored Sep 24, 2022
2 parents 5be39a0 + 96c8700 commit 2ece74b
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
46 changes: 46 additions & 0 deletions boj/class5/1208.py
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)
41 changes: 41 additions & 0 deletions boj/class5/1644.py
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)
35 changes: 35 additions & 0 deletions boj/class5/16566.py
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)
23 changes: 23 additions & 0 deletions boj/class5/2143.py
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)

0 comments on commit 2ece74b

Please sign in to comment.