QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#742355#8058. Binary vs Ternaryquark404#RE 15ms10564kbPython33.2kb2024-11-13 16:26:452024-11-13 16:26:46

Judging History

你现在查看的是最新测评结果

  • [2024-11-13 16:26:46]
  • 评测
  • 测评结果:RE
  • 用时:15ms
  • 内存:10564kb
  • [2024-11-13 16:26:45]
  • 提交

answer

ans = []

def count_leading_zero(s: str) -> int:
    res = 0
    for c in s:
        if (c == '0'): res += 1
        else: break
    return res

def modify(s: str, l: int, r: int, base: int = 0) -> str:
    assert(base == 0)
    if (l >= r): return s
    src = s[l:r + 1]
    src = int(src, base=3)
    src = bin(src)[2:]
    ans.append((l + 1 + base, r + 1 + base, f"before: {s}, after: {s[:l] + src + s[r + 1:]}"))
    return s[:l] + src + s[r + 1:]

def erase_trailing_zero(s: str, base: int) -> str:
    l, r = len(s) - 1, len(s) - 1
    while (l >= 0 and s[l] == '0'): l -= 1
    l += 1
    assert(l >= 1)
    return modify(s, l, r, base)

def change_last_digit(s: str, base: int):
    l = len(s) - 2
    while (s[l] == '0'): l -= 1
    assert(l >= 1)
    assert(len(s) >= 4)
    s = modify(s, l, len(s) - 1, base)
    return s
    # if (s[-1] == '1'):
    #     s = s[:-1]
    #     s = erase_trailing_zero(s, base)
    #     s = s + '1'
    #     if (s[:-2] == '0'):
    #         return modify(s, len(s) - 3, len(s) - 1, base)
    #     else:
    #         return modify(s, len(s) - 2, len(s) - 1, base)
    # else:
    #     s = erase_trailing_zero(s, base)
    #     return modify(s, len(s) - 2, len(s) - 1, base)

def cleanup(s: str, base: int):

    for i in range(1, len(s)):
        if (s[i] == '0'):
            s = modify(s, i - 1, i, base)
    for i in range(len(s) - 1, 0, -1):
        s = modify(s, i - 1, i, base)
    s = erase_trailing_zero(s, base)
    return s

def solve() -> None:
    ans.clear()
    s = input()
    t = input()

    # 1) remove all leading 0's
    s_lz = count_leading_zero(s)
    t_lz = count_leading_zero(t)
    if (s_lz < t_lz):
        print(-1)
        return
    s = modify(s, 0, s_lz - t_lz)
    base = count_leading_zero(s)
    s, t = s[base:], t[base:]
    # print("1st: ", s, t)


    # 2) check if s == 1 or t == 1
    if (len(s) == 1 or len(t) == 1):
        if (s != t):
            print(-1)
        else:
            print(len(ans))
            for (l, r) in ans:
                print(l, r)
        return
    # print("2nd: ", s, t)

    assert (len(s) >= 2)
    # 至多操作两次长度即为3
    if (len(s) == 2): s = modify(s, 0, 1, base)
    if (len(s) == 2): s = modify(s, 0, 1, base)
    assert (len(s) >= 3)

    # 3) expand s to length(t)
    while (len(t) > 2):
        assert(len(s) >= 3)
        # 保证第2位是1且长度至少为4
        if (len(s) == 3): s = modify(s, 0, 2, base)
        if (s[1] == 0): s = modify(s, 0, 1, base)
        while (s[-1] != t[-1]):
            s = change_last_digit(s, base)
        s, t = s[:-1], t[:-1]

        s = erase_trailing_zero(s, base)

        # print("3rd in progress: ", s, t)    
    
    # print("3rd: ", s, t)

    # 4) cleanup
    s = cleanup(s, base)

    # print("4th: ", s, t)

    # 5) check if s != t
    if (t == '11'):
        s = modify(s, 0, 1, base)

    # if (len(s) > 512): assert(0)

    print(len(ans))
    for (l, r, debug) in ans:
        print(l, r)
        # print(debug)
    

def main():
    t = int(input())
    for _ in range(t): solve()

if (__name__ == "__main__"):
    main()

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 15ms
memory: 10564kb

input:

3
1
111
110110
1101010
1111
111111

output:

-1
7
2 4
1 3
2 3
2 3
1 2
2 5
1 2
12
1 3
1 3
2 4
3 4
1 3
2 4
3 4
2 3
2 3
1 2
2 5
1 2

result:

ok Haitang Suki (3 test cases)

Test #2:

score: -100
Dangerous Syscalls

input:

1000
11100
111
1
11110
10001
10
1011
1111
10
1110
1100
11
11010
11
110
11
1
10001
10110
10
10
11111
10000
1001
10
1
11
10111
11
10
1
100
11
10100
1
10
101
11
1100
110
11
1110
1
1001
1
11111
10
10010
10
11001
110
1010
10011
1110
10100
1001
1001
101
100
1
1001
11
101
11
101
1001
1
1
1011
1
10
10
1011
...

output:

8
3 5
4 5
3 4
3 4
2 3
1 2
2 7
1 2
-1
8
1 2
2 3
3 4
4 5
3 4
2 3
1 2
2 9
7
1 3
3 4
1 2
2 3
1 2
2 5
1 2
9
1 2
1 2
1 3
1 4
3 4
2 3
1 2
2 5
1 2
7
2 3
3 4
3 4
2 3
1 2
2 7
1 2
8
2 3
4 5
4 5
3 4
2 3
1 2
2 9
1 2
5
2 3
2 3
1 2
2 5
1 2
-1
7
1 2
4 5
4 5
3 4
2 3
1 2
2 9

result: