結果

問題 No.1820 NandShift
ユーザー shotoyooshotoyoo
提出日時 2022-01-21 23:28:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 3,325 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 86,936 KB
実行使用メモリ 77,684 KB
最終ジャッジ日時 2023-08-17 08:14:50
合計ジャッジ時間 5,525 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,328 KB
testcase_01 AC 68 ms
71,388 KB
testcase_02 AC 70 ms
71,400 KB
testcase_03 AC 69 ms
71,456 KB
testcase_04 AC 98 ms
77,220 KB
testcase_05 AC 67 ms
71,504 KB
testcase_06 AC 74 ms
75,832 KB
testcase_07 AC 91 ms
76,716 KB
testcase_08 AC 69 ms
71,392 KB
testcase_09 AC 81 ms
75,944 KB
testcase_10 AC 96 ms
76,968 KB
testcase_11 AC 69 ms
71,368 KB
testcase_12 AC 87 ms
76,332 KB
testcase_13 AC 184 ms
77,508 KB
testcase_14 AC 72 ms
75,452 KB
testcase_15 AC 93 ms
77,340 KB
testcase_16 AC 201 ms
77,504 KB
testcase_17 AC 77 ms
76,520 KB
testcase_18 AC 141 ms
77,580 KB
testcase_19 AC 215 ms
77,568 KB
testcase_20 AC 79 ms
76,408 KB
testcase_21 AC 85 ms
76,756 KB
testcase_22 AC 215 ms
77,456 KB
testcase_23 AC 78 ms
76,688 KB
testcase_24 AC 162 ms
77,448 KB
testcase_25 AC 220 ms
77,684 KB
testcase_26 AC 78 ms
76,560 KB
testcase_27 AC 94 ms
77,212 KB
testcase_28 AC 223 ms
77,492 KB
testcase_29 AC 67 ms
71,412 KB
testcase_30 AC 232 ms
77,676 KB
testcase_31 AC 78 ms
76,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))
debug = lambda x: sys.stderr.write(x+"\n")
YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO)
LI = lambda : list(map(int, input().split()))
# sys.setrecursionlimit(3*10**5+10)

class BaseF2:
    def  __init__(self, vs=None):
        """vs: list of vecotr in F2
        orig[i] : i番目の基底に対応する入力された整数
        index[i]: i番目の基底を作るときにinsertした数を復元するために必要なbaseの要素のインデックス
        """
        self.base = [0]
        self.orig = [0]
        self.index = [0]
        if vs is not None:
            for v in vs:
                self.insert(v)
    def dim(self):
        return len(self.base)
    def insert(self, v):
        # 次元が上がったかを返す
        v0 = v
        ind = 0
        for i,u in enumerate(self.base):
            if v^u < v:
                v = v^u
                ind ^= self.index[i]
            if v==0:
                return False
        ind ^= (1<<len(self.base))
        self.base.append(v)
        self.orig.append(v0)
        self.index.append(ind)
        return True
    def check(self, v):
        res = [] # vを作るためのbaseの整数の組み合わせ方
        b = 0 # vを作るためのorigの整数の組み合わせ方
        for i,u in enumerate(self.base):
            nv = min(v, v^u)
            if nv<v:
                res.append(i)
                b ^= self.index[i]
                v = nv
        return v==0 , res, b
    def clear(self):
        self.base = []

n,m = list(map(int, input().split()))
x = int(input(),2)
a = [int(input(),2) for _ in range(n)]
n += 1
base = BaseF2()
mask = (1<<m) - 1
a.append(mask)
l = []
for b in range(2):
    for i in range(m):
        for j in range(n):
            if b:
                tmp = a[j]^mask
            else:
                tmp = a[j]
            val = (tmp<<i)&mask
            if val:
                if base.insert(val):
                    l.append((val, i, j, b))
def xor(i,j):
    global ind
    ll.append((2, ind, i, j))
    ind0 = ind
    ind += 1
    ll.append((2, ind, ind0, i))
    ind1 = ind
    ind += 1
    ll.append((2, ind, ind0, j))
    ind2 = ind
    ind += 1
    ll.append((2, 0, ind1, ind2))
    _a[0] = _a[i]^_a[j]
def shift(i):
    ll.append((1, i, i))
    _a[i] <<= 1

f, res, b = base.check(x)
if not f:
    print(-1)
else:
    ans = []
    ss = [[] for _ in range(2*n+1)]
    xx = 0
    used = 0
    ll = []
    ind = 1000
    _a = [0] + a + [0]*10000
    ll.append((2, n, 1000, 1000))
    for i in range(len(base.orig)):
        if b>>i&1:
            ans.append(l[i-1])
            v,ii,jj,bb = l[i-1]
            jj += 1
            if bb:
                jj += n
            ss[jj].append(ii)
            xx ^= v
    for i in range(1, n+1):
        ll.append((2, i+n, i, i+n))
        _a[i+n] = _a[i]^mask
    assert xx==x
    for i in range(1, 2*n+1):
        tmp = ss[i]
        tmp.sort()
        p = 0
        for k in tmp:
            for _ in range(k-p):
                shift(i)
            p = k
            xor(0, i)
#     assert _a[0]==x
    print(len(ll))
    for item in ll:
        write(" ".join(map(str, item)))
0