結果
問題 | No.1969 XOR Equation |
ユーザー | chineristAC |
提出日時 | 2022-02-09 11:40:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 577 ms / 2,000 ms |
コード長 | 2,844 bytes |
コンパイル時間 | 232 ms |
コンパイル使用メモリ | 82,108 KB |
実行使用メモリ | 92,536 KB |
最終ジャッジ日時 | 2024-09-21 03:29:17 |
合計ジャッジ時間 | 15,883 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
60,796 KB |
testcase_01 | AC | 61 ms
65,168 KB |
testcase_02 | AC | 61 ms
68,644 KB |
testcase_03 | AC | 56 ms
65,292 KB |
testcase_04 | AC | 58 ms
65,204 KB |
testcase_05 | AC | 60 ms
66,992 KB |
testcase_06 | AC | 58 ms
66,792 KB |
testcase_07 | AC | 60 ms
67,160 KB |
testcase_08 | AC | 58 ms
66,344 KB |
testcase_09 | AC | 429 ms
78,232 KB |
testcase_10 | AC | 433 ms
78,224 KB |
testcase_11 | AC | 488 ms
80,324 KB |
testcase_12 | AC | 474 ms
80,428 KB |
testcase_13 | AC | 448 ms
80,108 KB |
testcase_14 | AC | 408 ms
78,120 KB |
testcase_15 | AC | 543 ms
92,396 KB |
testcase_16 | AC | 547 ms
92,476 KB |
testcase_17 | AC | 546 ms
92,252 KB |
testcase_18 | AC | 540 ms
91,756 KB |
testcase_19 | AC | 542 ms
92,328 KB |
testcase_20 | AC | 435 ms
78,364 KB |
testcase_21 | AC | 476 ms
79,112 KB |
testcase_22 | AC | 522 ms
83,972 KB |
testcase_23 | AC | 425 ms
78,192 KB |
testcase_24 | AC | 462 ms
79,164 KB |
testcase_25 | AC | 522 ms
91,988 KB |
testcase_26 | AC | 530 ms
92,320 KB |
testcase_27 | AC | 537 ms
92,288 KB |
testcase_28 | AC | 544 ms
92,208 KB |
testcase_29 | AC | 540 ms
92,224 KB |
testcase_30 | AC | 541 ms
92,404 KB |
testcase_31 | AC | 539 ms
92,328 KB |
testcase_32 | AC | 547 ms
92,132 KB |
testcase_33 | AC | 541 ms
92,536 KB |
testcase_34 | AC | 545 ms
92,152 KB |
testcase_35 | AC | 577 ms
79,428 KB |
testcase_36 | AC | 203 ms
91,328 KB |
ソースコード
def solve_mini(N,A,Y,M=60): dp = [[1<<(M+1) for j in range(N+1)] for i in range(M+1)] for j in range(Y>>M,N+1,2): dp[M][j] = 0 low_sort = [[i for i in range(N)] for _ in range(M)] for t in range(M): low_sort[t].sort(key=lambda i:A[i]%(1<<(t+1)),reverse=True) """ pre = [i for i in range(N)] for t in range(M): zero,one = [],[] for i in pre: if A[i]>>t & 1: one.append(i) else: zero.append(i) pre = one + zero low_sort[t] = [i for i in pre] """ for i in range(M)[::-1]: zero,one,two = 0,0,0 for j in range(N): if A[j]>>i & 1: one += 1 else: zero += 1 if one&1==(Y>>i & 1): dp[i][0] = min(dp[i][0], 2*dp[i+1][two]) if (zero+two)&1==(Y>>i & 1): dp[i][0] = min(dp[i][0], 2*dp[i+1][one+two]+1) if i==0: break for j in range(1,N+1): next_up = low_sort[i-1][j-1] if A[next_up]>>i & 1: one -= 1 two += 1 else: zero -= 1 one += 1 if j!=N: next_next_up = low_sort[i-1][j] if A[next_up]%(1<<(t+1)) == A[next_next_up]%(1<<(t+1)): continue if one&1==(Y>>i & 1): dp[i][j] = min(dp[i][j], 2*dp[i+1][two]) if (zero+two)&1==(Y>>i & 1): dp[i][j] = min(dp[i][j], 2*dp[i+1][one+two]+1) if dp[0][0]==1<<(M+1): return -1 return dp[0][0] def brute(N,A,Y): ans = 2**61 def dfs(d,tmp,up): nonlocal ans if d==61: if len(up)&1==0: return tmp else: return 1<<61 res = ans if tmp > res: return ans zero,one,two = 0,set(),set() for i in range(N): if A[i]>>d & 1: if i in up: two.add(i) else: one.add(i) else: if i in up: one.add(i) else: zero += 1 if len(one)&1==(Y>>d)&1: res = min(res,dfs(d+1,tmp,two)) if (zero+len(two))&1==(Y>>d)&1 and res > tmp + (1<<d): n_up = set([i for i in one]+[j for j in two]) res = min(res,dfs(d+1,tmp+(1<<d),n_up)) ans = min(ans,res) return res res = dfs(0,0,set()) if res < 2**61: return res return -1 for _ in range(int(input())): N,Y = map(int,input().split()) A = list(map(int,input().split())) print(solve_mini(N,A,Y))