結果

問題 No.3739 Stronger Network
コンテスト
ユーザー hirayuu_yc
提出日時 2026-09-19 16:49:34
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,981 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 63 ms
コンパイル使用メモリ 82,064 KB
実行使用メモリ 115,380 KB
最終ジャッジ日時 2026-09-19 16:49:42
合計ジャッジ時間 7,481 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def graycode(l,r):
    if l==1:
        return [0]
    if l==2:
        return [0,1]
    if l//2<=r:
        ret=graycode(l//2,r-l//2)
        ret=ret[::-1]+ret
        for i in range(l//2,l):
            ret[i]^=l//2
        return ret
    else:
        ret=graycode(l//2,r)
        ret=ret+ret[::-1]
        for i in range(l//2,l):
            ret[i]^=l//2
        return ret

def nibeki(i):
    ret=2
    while ret<i:
        ret*=2
    return ret
    
def sw(x,p,q):
    lx=len(x)
    for i in range(lx):
        pa=x[i]&p
        qa=x[i]&q
        x[i]|=p
        x[i]-=p
        x[i]|=q
        x[i]-=q
        if qa:
            x[i]|=p
        if pa:
            x[i]|=q
    return x

def grc(h):
    m=nibeki(h)
    if m==h:
        return graycode(m,0)
    m//=2
    sm=grc(h-m)
    gc=graycode(m,0)
    
    sd=sm[0]^sm[-1]
    gd=gc[0]^gc[-1]
    
    sw(gc,gd,sd)
    
    df=gc[-1]^sm[0]
    
    for i in range(m):
        gc[i]^=df
    
    for i in range(h-m):
        sm[i]+=m
        gc.append(sm[i])
    
    return gc
    
    
H,W=map(int,input().split())
if H%2==1 or W%2==1:
	print(-1)
	exit()
h=grc(H)
w=grc(W)
hh=nibeki(H-1)
ww=nibeki(W-1)

hh//=2
ww//=2
hs=[]
ws=[]
while hh:
    hs.append((hh,(H-1)&hh))
    hh//=2

while ww:
    ws.append((ww,(W-1)&ww))
    ww//=2


hp=[]
for i in hs:
    if hp and i[1]:
        break
    hp.append((i[0],0))
wp=[]
for i in ws:
    if wp and i[1]:
        break
    wp.append((i[0],1))
if len(hp)<len(wp):
    hp,wp=wp,hp
order=hp+wp
for i in hs:
    if (i[0],0) not in order:
        order.append((i[0],0))
for i in ws:
    if (i[0],1) not in order:
        order.append((i[0],1))


ans=[[0]*W for i in range(H)]

for i in range(H):
    for j in range(W):
        v=pow(2,len(order)-1)
        for b,p in order:
            if p==0:
                if h[i]&b:
                    ans[i][j]|=v
            else:
                if w[j]&b:
                    ans[i][j]|=v
            v//=2
for i in ans:
    print(*i)
0