結果

問題 No.74 貯金箱の退屈
コンテスト
ユーザー ckawatak
提出日時 2018-12-23 12:54:18
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 1,309 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 370 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 15,380 KB
最終ジャッジ日時 2026-04-12 14:58:40
合計ジャッジ時間 4,733 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N = int(input())
D = list(map(int, input().split(' ')))
W = list(map(int, input().split(' ')))

def unite(n, graphs, pairs):
    if n == len(pairs):
        return graphs

    pair = pairs[n]
    united_graph = {}
    new_graphs = []
    for graph in graphs:
        if pair[0] in graph or pair[1] in graph:
            united_graph.update(graph)
        else:
            new_graphs.append(graph)

    united_graph.update({pair[0]:1, pair[1]:1})

    # flag to indicate that the same coin is flipped
    if pair[0] == pair[1]:
        united_graph[N] = 1

    new_graphs.append(united_graph)

    return unite(n+1, new_graphs, pairs)

normalized = []
for d in D:
    normalized.append(d % N)

# find coins to be flipped    
pairs = []
for i,n in enumerate(normalized):
    pairs.append( ( (i + n + N) % N, (i - n + N) % N ) )

indices = [0 for _ in range(N)]
for pair in pairs:
    indices[pair[0]] = 1
    indices[pair[1]] = 1

# find coins not to be flipped and 0    
for i in range(N):
    if indices[i] == 0 and W[i] == 0:
        print('No')
        exit(0)

graphs = unite(0, [], pairs)

for graph in graphs:
    zeros = 0
    for key in graph:
        if key < N and W[key] == 0:
            zeros = zeros + 1
    if zeros % 2 != 0 and N not in graph:
        print('No')
        exit(0)
print('Yes')
0