結果

問題 No.460 裏表ちわーわ
ユーザー cielciel
提出日時 2017-03-09 14:33:02
言語 PyPy2
(7.3.13)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 1,918 bytes
コンパイル時間 465 ms
コンパイル使用メモリ 78,216 KB
実行使用メモリ 81,876 KB
最終ジャッジ日時 2023-09-06 05:16:54
合計ジャッジ時間 5,366 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
78,860 KB
testcase_01 AC 74 ms
76,416 KB
testcase_02 AC 92 ms
79,668 KB
testcase_03 AC 74 ms
76,256 KB
testcase_04 AC 74 ms
76,420 KB
testcase_05 AC 82 ms
78,468 KB
testcase_06 AC 84 ms
78,408 KB
testcase_07 AC 83 ms
78,596 KB
testcase_08 AC 171 ms
81,664 KB
testcase_09 AC 85 ms
79,140 KB
testcase_10 AC 170 ms
81,460 KB
testcase_11 AC 83 ms
78,800 KB
testcase_12 AC 84 ms
78,424 KB
testcase_13 AC 172 ms
81,464 KB
testcase_14 AC 172 ms
81,464 KB
testcase_15 AC 171 ms
81,876 KB
testcase_16 AC 84 ms
78,656 KB
testcase_17 AC 172 ms
81,548 KB
testcase_18 AC 171 ms
81,624 KB
testcase_19 AC 172 ms
81,800 KB
testcase_20 AC 83 ms
78,624 KB
testcase_21 AC 172 ms
81,348 KB
testcase_22 AC 172 ms
81,620 KB
testcase_23 AC 171 ms
81,836 KB
testcase_24 AC 169 ms
81,616 KB
testcase_25 AC 74 ms
76,696 KB
testcase_26 AC 73 ms
76,508 KB
testcase_27 AC 74 ms
76,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
#coding:utf-8
import sys
if sys.version_info[0]<3:
	input=raw_input
	range=xrange

def popcnt(n):
	r=0
	while n>0:
		r+=n%2
		n//=2
	return r

def lightsout(x,y):
	a=[[0,0] for _ in range(x*y)]

	#create problem
	for i in range(x):
		for j in range(y):
			a[i+j*x][0]=1<<(i+j*x)
			a[i+j*x][1]= 0 +\
				(1<<(i+j*x)) |\
				(1<<(i-1+j*x) if i>0   else 0) |\
				(1<<(i+1+j*x) if i<x-1 else 0) |\
				(1<<(i+(j-1)*x) if j>0   else 0) |\
				(1<<(i+(j+1)*x) if j<y-1 else 0) |\
				(1<<(i-1+(j-1)*x) if i>0   and j>0   else 0) |\
				(1<<(i+1+(j-1)*x) if i<x-1 and j>0   else 0) |\
				(1<<(i-1+(j+1)*x) if i>0   and j<y-1 else 0) |\
				(1<<(i+1+(j+1)*x) if i<x-1 and j<y-1 else 0) |\
				0

	#solve
	badbits=0
	for i in range(x*y):
		if (a[i][1]&(1<<i))==0:
			for j in range(i+1,x*y):
				if (a[j][1]&(1<<i))!=0:
					a[i],a[j]=a[j],a[i]
					break
			else:
				badbits|=1<<i
				continue
		for j in range(x*y):
			if i==j: continue
			if (a[j][1]&(1<<i))!=0:
				a[j][0]^=a[i][0]
				a[j][1]^=a[i][1]

	k=x*y-popcnt(badbits)
	#STDERR.puts "quiet pattern=%d"%[x*y-k]

	#0解(quiet pattern)の集合tを用意する
	tmsk=0
	t=[]
	a_ok=[]
	for i in range(x*y):
		if ((badbits>>i)&1):
			t.append(a[i][0])
		else:
			a_ok.append((i,a[i][0]))
			tmsk|=1<<i

	#入力・解の存在判定
	input_=0
	for j in range(y):
		a=list(map(int,input().split()))
		for i in range(x):
			input_|=a[i]<<j*x+i
	if any(popcnt(e&input_)%2 for e in t):
		return -1

	tlst=[0]*(1<<(x*y-k))
	for l in range(1<<(x*y-k)):
		r=0
		for j in range(x*y-k):
			if (l&(1<<j)):
				r^=t[j]
		tlst[l]=r

	r0=1<<29
	c0=0
	for j in a_ok:
		if (input_&(1<<j[0])):
			c0^=j[1]
	#0解の重ね合わせをすべて試す
	for l in range(1<<(x*y-k)):
		r1=c0
		r1^=tlst[l]
		r0=min(r0,popcnt(r1))
	return r0

try:
	while True:
		m,n=map(int,input().split())
		r=lightsout(n,m)
		print('Impossible' if r<0 else r)
except EOFError:
	pass
0