ํ ๋ฒ ๊ณ์ฐ๋ ๋ฌธ์ ๋ ๋ค์ ๊ณ์ฐํ์ง ์๋๋ก ํ๋ค.
# Memoization (Top-Down, ํํฅ์)
# ํ ๋ฒ ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ๋ชจ์ด์ ์ด์
ํ๊ธฐ ์ํ ๋ฆฌ์คํธ ์ด๊ธฐํ
d = [0]*100
# ํผ๋ณด๋์น ํจ์๋ฅผ ์ฌ๊ทํจ์๋ก ๊ตฌํ(ํ๋ค์ด ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ)
def fibo(x):
# ์ข
๋ฃ ์กฐ๊ฑด(1 ํน์ 2์ผ ๋ 1์ ๋ฐํ)
if x == 1 or x == 2:
return 1
# ์ด๋ฏธ ๊ณ์ฐํ ์ ์๋ ๋ฌธ์ ๋ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
if d[x] != 0:
return d[x]
# ๋ง์ฝ ๊ณ์ฐํ ์ ์ด ์๋ค๋ฉด ์ ํ์์ ๋ฐ๋ผ์ ํผ๋ณด๋์น ๊ฒฐ๊ณผ ๋ฐํ
d[x] = fibo(x-1) + fibo(x-2)
return d[x]
print(fibo(99))
# Tabulation (Bottom-up, ์ํฅ์)
# ์์ ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๊ธฐ ์ํ DP ํ
์ด๋ธ ์ด๊ธฐํ
d = [0] * 100
# ์ฒซ ๋ฒ์งธ ํผ๋ณด๋์น ์์ ๋ ๋ฒ์งธ ํผ๋ณด๋์น ์๋ 1
d[1] = 1
d[2] = 1
n = 99
# ํผ๋ณด๋์น ํจ์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ตฌํ
for i in range(3, n+1):
d[i] = d[i - 1] + d[i - 2]
print(d[n])
<์ฒ์ ์๋>
n, m = map(int, input().split())
tot = [list(map(int, input().split())) for _ in range(n)]
k = int(input())
def solution(tot):
i, j, x, y = map(int, input().split())
sum = 0
for a in range(i, x+1):
for b in range(j, y+1):
sum += tot[a-1][b-1]
return sum
for _ in range(k):
print(solution(tot))
์๊ฐ ์ ํ 2์ด์ด๊ณ ์๊ฐ ๋ณต์ก๋ O(N^2)์ธ ๊ฒ ๊ฐ์๋ฐ ์ ์๊ฐ์ด๊ณผ๊ฐ ์๊ธธ๊น?
-> ์.. ํจ์๋ฅผ ํธ์ถํ๋๊น ์๊ฐ ๋ณต์ก๋๊ฐ O(N^3)์ด๊ฒ ๊ตฌ๋.. ํฐ ์ฐฉ๊ฐ์..
<์ฌ์๋>
n, m = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(n)]
# ๋์ ํฉ ํ
์ด๋ธ ๋ง๋ค๊ธฐ
dp = [[0]*(m+1) for _ in range(n+1)]
for a in range(1, n+1):
for b in range(1, m+1):
dp[a][b] = lst[a-1][b-1] + dp[a][b-1] + dp[a-1][b] - dp[a-1][b-1]
k = int(input())
for _ in range(k):
i, j, x, y = map(int, input().split())
print(dp[x][y] - dp[x][j-1] - dp[i-1][y] + dp[i-1][j-1])