Skip to content

Commit 7e3c501

Browse files
authored
Merge pull request #24 from team-mate-algorithm/jaykxo/week07
Jaykxo/week07
2 parents 45ab2c9 + 86660a2 commit 7e3c501

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 11659 구간 합 구하기 4
2+
# Python3, 메모리: 52312 KB, 시간: 204 ms
3+
# PyPy3, 메모리: 121852 KB, 시간: 144 ms
4+
5+
import sys
6+
input = sys.stdin.readline
7+
8+
N, M = map(int, input().strip().split())
9+
arr = list(map(int, input().split()))
10+
11+
# 누적합 배열 초기화 (0번째 인덱스는 0으로 패딩)
12+
prefix_sum = [0] * (N + 1)
13+
14+
# (1 ~ i)까지의 누적합 계산
15+
for i in range(1, N + 1):
16+
prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1]
17+
18+
answers = []
19+
20+
# (i ~ j) 구간합 = prefix_sum[j] - prefix_sum[i - 1]
21+
for _ in range(M):
22+
i, j = map(int, input().split())
23+
s = prefix_sum[j] - prefix_sum[i - 1]
24+
answers.append(s)
25+
26+
print('\n'.join(map(str, answers)))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 11660 구간 합 구하기 5
2+
# Python3, 메모리: 116164 KB, 시간: 712 ms
3+
# PyPy3, 메모리: 136748 KB, 시간: 256 ms
4+
5+
import sys
6+
input = sys.stdin.readline
7+
8+
# 한 번 함수로 맨들어 보고 싶어서 해봤읍니다,,,,
9+
def build_prefix(arr):
10+
"""2차원 누적합 테이블 생성"""
11+
N = len(arr) - 1
12+
ps = [[0] * (N + 1) for _ in range(N + 1)]
13+
for i in range(1, N + 1):
14+
for j in range(1, N + 1):
15+
# (1,1)~(i,j) 구간합 = 위 + 왼쪽 - 겹친부분 + 현재값
16+
ps[i][j] = ps[i-1][j] + ps[i][j-1] - ps[i-1][j-1] + arr[i][j]
17+
return ps
18+
19+
def range_sum(ps, x1, y1, x2, y2):
20+
"""(x1,y1)~(x2,y2) 구간합 반환"""
21+
# 전체 - 위쪽 - 왼쪽 + 겹친부분
22+
return ps[x2][y2] - ps[x1-1][y2] - ps[x2][y1-1] + ps[x1-1][y1-1]
23+
24+
# -------------------- main --------------------
25+
N, M = map(int, input().split())
26+
27+
# 1-index 접근을 위한 0 패딩
28+
arr = [[0] * (N + 1)]
29+
for _ in range(N):
30+
arr.append([0] + list(map(int, input().split())))
31+
32+
# 누적합 생성
33+
ps = build_prefix(arr)
34+
35+
# 구간합 계산
36+
out = []
37+
for _ in range(M):
38+
x1, y1, x2, y2 = map(int, input().split())
39+
out.append(str(range_sum(ps, x1, y1, x2, y2)))
40+
41+
print("\n".join(out))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 1929 소수 구하기
2+
# Python3, 메모리: 39656 KB, 시간: 3340 ms
3+
# PyPy3, 메모리: 116148 KB, 시간: 796 ms
4+
5+
import sys
6+
input = sys.stdin.readline
7+
8+
def is_prime(x):
9+
if x < 2:
10+
return False
11+
for j in range(2, int(x ** 0.5) + 1):
12+
if x % j == 0:
13+
return False
14+
return True # 위 조건 통과 시 소수
15+
16+
# -------------------- main --------------------
17+
18+
M, N = map(int, input().split())
19+
primes = []
20+
21+
for i in range(M, N + 1):
22+
if is_prime(i):
23+
primes.append(i)
24+
25+
print('\n'.join(map(str, primes)))

jaykxo/week07/2581_소수.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 2581 소수
2+
# Python3, 메모리: 32412 KB, 시간: 40 ms
3+
# PyPy3, 메모리: 110736 KB, 시간: 100 ms
4+
5+
import sys
6+
input = sys.stdin.readline
7+
8+
def is_prime(x):
9+
if x < 2:
10+
return False
11+
for j in range(2, int(x ** 0.5) + 1):
12+
if x % j == 0:
13+
return False
14+
return True # 위 조건 통과 시 소수
15+
16+
# -------------------- main --------------------
17+
18+
M = int(input())
19+
N = int(input())
20+
21+
primes = []
22+
23+
for i in range(M, N + 1):
24+
if is_prime(i):
25+
primes.append(i)
26+
27+
if not primes:
28+
print(-1)
29+
30+
else:
31+
print(sum(primes))
32+
print(min(primes))

0 commit comments

Comments
 (0)