問題はこれ→https://atcoder.jp/contests/abc165/tasks/abc165_c
結構有名だと思う。
普通の解法
再帰関数を使う。
https://atcoder.jp/contests/abc165/submissions/37076168
n, m, q = map(int, input().split())
a, b, c, d = [0] * q, [0] * q, [0] * q, [0] * q
for i in range(q):
a[i], b[i], c[i], d[i] = map(int, input().split())
a[i] -= 1
b[i] -= 1
def solve(x):
res = 0
if len(x) == n:
for i in range(q):
if x[b[i]] - x[a[i]] == c[i]:
res += d[i]
return res
if len(x) == 0:
bef = 1
else:
bef = x[-1]
for i in range(bef, m + 1):
res = max(res, solve(x + [i]))
return res
print(solve([]))
多重for(タイピング力w)
Nの値で場合分け。
n, m, q = map(int, input().split())
a = []
b = []
c = []
d = []
for _ in range(q):
A, B, C, D = map(int, input().split())
A -= 1
B -= 1
a.append(A)
b.append(B)
c.append(C)
d.append(D)
def score(t):
res = 0
for i, j, k, l in zip(a, b, c, d):
if t[j] - t[i] == k:
res += l
return res
ans = 0
if n == 2:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
ans = max(ans, score([i0, i1]))
if n == 3:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
ans = max(ans, score([i0, i1, i2]))
if n == 4:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
for i3 in range(i2, m + 1):
ans = max(ans, score([i0, i1, i2, i3]))
if n == 5:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
for i3 in range(i2, m + 1):
for i4 in range(i3, m + 1):
ans = max(ans, score([i0, i1, i2, i3, i4]))
if n == 6:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
for i3 in range(i2, m + 1):
for i4 in range(i3, m + 1):
for i5 in range(i4, m + 1):
ans = max(ans, score([i0, i1, i2, i3, i4, i5]))
if n == 7:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
for i3 in range(i2, m + 1):
for i4 in range(i3, m + 1):
for i5 in range(i4, m + 1):
for i6 in range(i5, m + 1):
ans = max(ans, score([i0, i1, i2, i3, i4, i5, i6]))
if n == 8:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
for i3 in range(i2, m + 1):
for i4 in range(i3, m + 1):
for i5 in range(i4, m + 1):
for i6 in range(i5, m + 1):
for i7 in range(i6, m + 1):
ans = max(ans, score([i0, i1, i2, i3, i4, i5, i6, i7]))
if n == 9:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
for i3 in range(i2, m + 1):
for i4 in range(i3, m + 1):
for i5 in range(i4, m + 1):
for i6 in range(i5, m + 1):
for i7 in range(i6, m + 1):
for i8 in range(i7, m + 1):
ans = max(ans, score([i0, i1, i2, i3, i4, i5, i6, i7, i8]))
if n == 10:
for i0 in range(1, m + 1):
for i1 in range(i0, m + 1):
for i2 in range(i1, m + 1):
for i3 in range(i2, m + 1):
for i4 in range(i3, m + 1):
for i5 in range(i4, m + 1):
for i6 in range(i5, m + 1):
for i7 in range(i6, m + 1):
for i8 in range(i7, m + 1):
for i9 in range(i8, m + 1):
ans = max(ans, score([i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]))
print(ans)
3021Byte。あー疲れた💦💦💦💦
今日もABC頑張るか