Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions jaykxo/week07/11659_구간_합_구하기_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 11659 구간 합 구하기 4
# Python3, 메모리: 52312 KB, 시간: 204 ms
# PyPy3, 메모리: 121852 KB, 시간: 144 ms

import sys
input = sys.stdin.readline

N, M = map(int, input().strip().split())
arr = list(map(int, input().split()))

# 누적합 배열 초기화 (0번째 인덱스는 0으로 패딩)
prefix_sum = [0] * (N + 1)

# (1 ~ i)까지의 누적합 계산
for i in range(1, N + 1):
prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1]

answers = []

# (i ~ j) 구간합 = prefix_sum[j] - prefix_sum[i - 1]
for _ in range(M):
i, j = map(int, input().split())
s = prefix_sum[j] - prefix_sum[i - 1]
answers.append(s)

print('\n'.join(map(str, answers)))
41 changes: 41 additions & 0 deletions jaykxo/week07/11660_구간_합_구하기_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 11660 구간 합 구하기 5
# Python3, 메모리: 116164 KB, 시간: 712 ms
# PyPy3, 메모리: 136748 KB, 시간: 256 ms

import sys
input = sys.stdin.readline

# 한 번 함수로 맨들어 보고 싶어서 해봤읍니다,,,,
def build_prefix(arr):
"""2차원 누적합 테이블 생성"""
N = len(arr) - 1
ps = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(1, N + 1):
# (1,1)~(i,j) 구간합 = 위 + 왼쪽 - 겹친부분 + 현재값
ps[i][j] = ps[i-1][j] + ps[i][j-1] - ps[i-1][j-1] + arr[i][j]
return ps

def range_sum(ps, x1, y1, x2, y2):
"""(x1,y1)~(x2,y2) 구간합 반환"""
# 전체 - 위쪽 - 왼쪽 + 겹친부분
return ps[x2][y2] - ps[x1-1][y2] - ps[x2][y1-1] + ps[x1-1][y1-1]

# -------------------- main --------------------
N, M = map(int, input().split())

# 1-index 접근을 위한 0 패딩
arr = [[0] * (N + 1)]
for _ in range(N):
arr.append([0] + list(map(int, input().split())))

# 누적합 생성
ps = build_prefix(arr)

# 구간합 계산
out = []
for _ in range(M):
x1, y1, x2, y2 = map(int, input().split())
out.append(str(range_sum(ps, x1, y1, x2, y2)))

print("\n".join(out))
25 changes: 25 additions & 0 deletions jaykxo/week07/1929_소수_구하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 1929 소수 구하기
# Python3, 메모리: 39656 KB, 시간: 3340 ms
# PyPy3, 메모리: 116148 KB, 시간: 796 ms

import sys
input = sys.stdin.readline

def is_prime(x):
if x < 2:
return False
for j in range(2, int(x ** 0.5) + 1):
if x % j == 0:
return False
return True # 위 조건 통과 시 소수

# -------------------- main --------------------

M, N = map(int, input().split())
primes = []

for i in range(M, N + 1):
if is_prime(i):
primes.append(i)

print('\n'.join(map(str, primes)))
32 changes: 32 additions & 0 deletions jaykxo/week07/2581_소수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 2581 소수
# Python3, 메모리: 32412 KB, 시간: 40 ms
# PyPy3, 메모리: 110736 KB, 시간: 100 ms

import sys
input = sys.stdin.readline

def is_prime(x):
if x < 2:
return False
for j in range(2, int(x ** 0.5) + 1):
if x % j == 0:
return False
return True # 위 조건 통과 시 소수

# -------------------- main --------------------

M = int(input())
N = int(input())

primes = []

for i in range(M, N + 1):
if is_prime(i):
primes.append(i)

if not primes:
print(-1)

else:
print(sum(primes))
print(min(primes))