https://www.codetree.ai/missions/2/problems/max-of-xor/submissions
n, m = map(int,input().split())
arr = list(map(int,input().split()))
selected = []
ans = -1
def f(d):
global ans
if d == m:
s = 0
for item in selected:
s = s^item
ans = max(ans,s)
return
for i in range(n):
if arr[i] not in selected:
selected.append(arr[i])
f(d+1)
selected.pop()
f(0)
print(ans)
์ ๋ฐฉ๋ฒ์ผ๋ก ํ์์๋ค. ๋ค๋ง, ์ ๋ฐฉ๋ฒ์ ๋ฌธ์ ๊ฐ ์๋๋ฐ 1, 2, 3, 4, 5 ๊ฐ ์๊ณ , ์ฒ์์ 3์ ์ ํํ๋ค๋ฉด ๊ทธ ์ดํ์๋ 4,5 ๋ง ์ ํํด์ผ ํ๋ค. ํ์ง๋ง ์ ์ฝ๋๋ ๊ทธ๋ฌํ ๋ก์ง์ด ์๊ธฐ์ ์๊ฐ์ด๊ณผ๋ผ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ฒ ๋์๋ค.
n, m = map(int,input().split())
arr = list(map(int,input().split()))
selected = []
ans = -1
def f(d,k):
global ans
if d == m:
s = 0
for item in selected:
s = s^arr[item]
ans = max(ans,s)
return
for i in range(k,n):
if i not in selected:
selected.append(i)
f(d+1,i+1)
selected.pop()
f(0,0)
print(ans)
๋จ์ํ ํจ์ ์์ ๋ณ์๋ฅผ ํ๋ ์ถ๊ฐํ์ฌ ์์ ๋ณด๋ค ๋ค์ ์๋ ์ธ๋ฑ์ค๋ฅผ ์ ํํ ์ ์๋๋ก ๋ง๋ค๋ฉด ๊ทธ๋ง์ด๋ค.