# from decimal import *
# getcontext().prec = 500
import random
random.seed(22132137)
import sys
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
kStayStill = 0 #I'm winning or losing, play P
kCatchUp = 1 #I'm losing, play P with prob 0.24, else 'N'
kFreeWill = 2 #play P/K/N with prob 1/3
kSwiruj = 3 #I'm winning, play P with prob 0.24, else 'K'
#p = Decimal(0.24)
B = 1000
kWin = 1
kDraw = 0
kLost = -1
idd = '#'
def GetRes(me, opp):
if me == opp:
return kDraw
if me == 'P' and opp == 'K':
return kWin
if me == 'K' and opp == 'N':
return kWin
if me == 'N' and opp == 'P':
return kWin
return kLost
def NextState(cur, res, im_smaller):
if cur == kStayStill:
#assert res != kWin
if res == kDraw:
return kStayStill
return kFreeWill
if cur == kCatchUp or cur == kSwiruj:
if res == kDraw:
return cur
return kFreeWill
if res == kWin:
if im_smaller:
return kStayStill
else:
return kSwiruj
if res == kDraw:
return kFreeWill
if im_smaller:
return kStayStill
else:
return kCatchUp
def Batch(n, msg, my_state, their_state):
to_send = []
#to_xor = []
ind = 0
SCALE = 2 ** (2 * B)
step = 2 ** (2 * B - n)
my_val = 0
cur_pow = SCALE // 2
to_xor_encode = []
to_xor_decode = []
for c in msg:
to_xor_encode.append(random.randint(0, 1))
to_xor_decode.append(random.randint(0, 1))
if idd == 'A':
to_xor_encode, to_xor_decode = to_xor_decode, to_xor_encode
pos = 0
for c in msg:
if c == '0':
to_send.append(to_xor_encode[pos])
else:
to_send.append(to_xor_encode[pos] ^ 1)
pos = pos + 1
my_val += to_send[-1] * cur_pow
cur_pow //= 2
# print(cur_pow, to_send[-1])
# for c in range(kBuffer):
# to_send.append(random.random() % 2)
my_val += cur_pow
my_L = 0
my_R = SCALE
his_L = 0
his_R = SCALE
#eps = Decimal(2) ** (-(n + 100))
# eprint(idd, to_xor)
# eprint(idd, to_send)
#eprint(idd, my_val)
#eprint(idd, msg)
#eprint("stderr")
while my_L // step < my_R // step or his_L // step < his_R // step:
my_move = '$'
#eprint(idd, my_L, my_R)
#eprint(idd, his_L, his_R)
#eprint(idd, my_state, their_state)
if my_state == kStayStill:
my_move = 'P'
elif my_state == kCatchUp or my_state == kSwiruj:
my_M = (my_L * 76 + my_R * 24) // 100
if my_val < my_M:
my_move = 'P'
my_R = my_M
else:
if my_state == kCatchUp:
my_move = 'N'
else:
my_move = 'K'
my_L = my_M
else:
my_M1 = (2 * my_L + my_R) // 3
my_M2 = (my_L + 2 * my_R) // 3
if my_val < my_M1:
my_move = 'P'
my_R = my_M1
elif my_val < my_M2:
my_move = 'K'
my_L, my_R = my_M1, my_M2
else:
my_move = 'N'
my_L = my_M2
#eprint(idd, my_move)
print(my_move, flush=True)
their_move = input()
if their_state == kFreeWill:
thr1 = (2 * his_L + his_R) // 3
thr2 = (his_L + 2 * his_R) // 3
if their_move == 'P':
his_R = thr1
elif their_move == 'K':
his_L, his_R = thr1, thr2
else:
his_L = thr2
elif their_state == kCatchUp or their_state == kSwiruj:
his_M = (his_L * 76 + his_R * 24) // 100
if their_move == 'P':
his_R = his_M
else:
his_L = his_M
res = GetRes(my_move, their_move)
A_wid = my_R - my_L
B_wid = his_R - his_L
if idd == 'B':
A_wid, B_wid = B_wid, A_wid
im_smaller = A_wid < B_wid
if idd == 'B':
im_smaller = not im_smaller
my_state = NextState(my_state, res, im_smaller)
their_state = NextState(their_state, -res, not im_smaller)
their_str = ""
for i in range(n):
his_L *= 2
if his_L > SCALE:
if to_xor_decode[i]:
their_str += '0'
else:
their_str += '1'
his_L -= SCALE
else:
if to_xor_decode[i]:
their_str += '1'
else:
their_str += '0'
#eprint(idd, their_str)
return their_str, my_state, their_state
def Test(n):
my_str = input()
their_str = ""
#eprint(idd, my_str)
my_state = kFreeWill
their_state = kFreeWill
while len(my_str):
L = min(B, len(my_str))
tstr, my_state, their_state = Batch(L, my_str[0 : L], my_state, their_state)
their_str += tstr
my_str = my_str[L:]
# eprint(idd, their_str)
print("! " + their_str, flush=True)
imie = input()
idd = imie[0]
# if s == "Algosia":
# print("! 00001")
# print("! 10001")
# else:
# print("! 10010")
# print("! 00010")
n, t = map(int, input().split())
for _ in range(t):
Test(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | # from decimal import * # getcontext().prec = 500 import random random.seed(22132137) import sys def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) kStayStill = 0 #I'm winning or losing, play P kCatchUp = 1 #I'm losing, play P with prob 0.24, else 'N' kFreeWill = 2 #play P/K/N with prob 1/3 kSwiruj = 3 #I'm winning, play P with prob 0.24, else 'K' #p = Decimal(0.24) B = 1000 kWin = 1 kDraw = 0 kLost = -1 idd = '#' def GetRes(me, opp): if me == opp: return kDraw if me == 'P' and opp == 'K': return kWin if me == 'K' and opp == 'N': return kWin if me == 'N' and opp == 'P': return kWin return kLost def NextState(cur, res, im_smaller): if cur == kStayStill: #assert res != kWin if res == kDraw: return kStayStill return kFreeWill if cur == kCatchUp or cur == kSwiruj: if res == kDraw: return cur return kFreeWill if res == kWin: if im_smaller: return kStayStill else: return kSwiruj if res == kDraw: return kFreeWill if im_smaller: return kStayStill else: return kCatchUp def Batch(n, msg, my_state, their_state): to_send = [] #to_xor = [] ind = 0 SCALE = 2 ** (2 * B) step = 2 ** (2 * B - n) my_val = 0 cur_pow = SCALE // 2 to_xor_encode = [] to_xor_decode = [] for c in msg: to_xor_encode.append(random.randint(0, 1)) to_xor_decode.append(random.randint(0, 1)) if idd == 'A': to_xor_encode, to_xor_decode = to_xor_decode, to_xor_encode pos = 0 for c in msg: if c == '0': to_send.append(to_xor_encode[pos]) else: to_send.append(to_xor_encode[pos] ^ 1) pos = pos + 1 my_val += to_send[-1] * cur_pow cur_pow //= 2 # print(cur_pow, to_send[-1]) # for c in range(kBuffer): # to_send.append(random.random() % 2) my_val += cur_pow my_L = 0 my_R = SCALE his_L = 0 his_R = SCALE #eps = Decimal(2) ** (-(n + 100)) # eprint(idd, to_xor) # eprint(idd, to_send) #eprint(idd, my_val) #eprint(idd, msg) #eprint("stderr") while my_L // step < my_R // step or his_L // step < his_R // step: my_move = '$' #eprint(idd, my_L, my_R) #eprint(idd, his_L, his_R) #eprint(idd, my_state, their_state) if my_state == kStayStill: my_move = 'P' elif my_state == kCatchUp or my_state == kSwiruj: my_M = (my_L * 76 + my_R * 24) // 100 if my_val < my_M: my_move = 'P' my_R = my_M else: if my_state == kCatchUp: my_move = 'N' else: my_move = 'K' my_L = my_M else: my_M1 = (2 * my_L + my_R) // 3 my_M2 = (my_L + 2 * my_R) // 3 if my_val < my_M1: my_move = 'P' my_R = my_M1 elif my_val < my_M2: my_move = 'K' my_L, my_R = my_M1, my_M2 else: my_move = 'N' my_L = my_M2 #eprint(idd, my_move) print(my_move, flush=True) their_move = input() if their_state == kFreeWill: thr1 = (2 * his_L + his_R) // 3 thr2 = (his_L + 2 * his_R) // 3 if their_move == 'P': his_R = thr1 elif their_move == 'K': his_L, his_R = thr1, thr2 else: his_L = thr2 elif their_state == kCatchUp or their_state == kSwiruj: his_M = (his_L * 76 + his_R * 24) // 100 if their_move == 'P': his_R = his_M else: his_L = his_M res = GetRes(my_move, their_move) A_wid = my_R - my_L B_wid = his_R - his_L if idd == 'B': A_wid, B_wid = B_wid, A_wid im_smaller = A_wid < B_wid if idd == 'B': im_smaller = not im_smaller my_state = NextState(my_state, res, im_smaller) their_state = NextState(their_state, -res, not im_smaller) their_str = "" for i in range(n): his_L *= 2 if his_L > SCALE: if to_xor_decode[i]: their_str += '0' else: their_str += '1' his_L -= SCALE else: if to_xor_decode[i]: their_str += '1' else: their_str += '0' #eprint(idd, their_str) return their_str, my_state, their_state def Test(n): my_str = input() their_str = "" #eprint(idd, my_str) my_state = kFreeWill their_state = kFreeWill while len(my_str): L = min(B, len(my_str)) tstr, my_state, their_state = Batch(L, my_str[0 : L], my_state, their_state) their_str += tstr my_str = my_str[L:] # eprint(idd, their_str) print("! " + their_str, flush=True) imie = input() idd = imie[0] # if s == "Algosia": # print("! 00001") # print("! 10001") # else: # print("! 10010") # print("! 00010") n, t = map(int, input().split()) for _ in range(t): Test(n) |
English