M = [-1] * (1 << N) M[x] = 0 Q = [x] while M[0] == -1 and len(Q) > 0: #print("Q = ", Q) s = Q.pop(0) for i in range(N - K + 1): xx = flip(s, i, K) if M[xx] == -1: M[xx] = M[s] + 1 Q.append(xx) return M[0] ifile, ofile = gcj.get_files('A') T = int(ifile.readline().strip()) for t in range(T): fields = ifile.readline().split() P = fields[0] N = len(P) K = int(fields[1]) x = 0 B = [0] * N for p in P: x *= 2 if p == '-': x += 1 for i in range(N): if P[i] == '+':
import gcj ifile, ofile = gcj.get_files('D') T = int(ifile.readline().strip()) for t in range(T): K, C, S = map(int, ifile.readline().strip().split(' ')) ans = ' '.join(map(str, range(1, K + 1))) gcj.print_answer(ofile, t, ans)
import gcj ifile, ofile = gcj.get_files('C') T = int(ifile.readline().strip()) for t in range(T): N = int(ifile.readline().strip()) F, S = [], [] for i in range(N): first, second = ifile.readline().strip().split(' ') F.append(first) S.append(second) mfake = 0 for x in range(1 << N): Ff, Sf = [], [] Fr, Sr = [], [] for i in range(N): if (x >> i) & 1: Fr.append(F[i]) Sr.append(S[i]) else: Ff.append(F[i]) Sf.append(S[i]) #print(Ff, Sf, ':', Fr, Sr) Nr = len(Fr) Nf = len(Ff)
import gcj, itertools ifile, ofile = gcj.get_files('B') T = int(ifile.readline().strip()) for t in range(T): C, J = ifile.readline().strip().split(' ') CJ = C + J missed = CJ.count('?') mn = pow(10, len(CJ)) Cm = pow(10, len(CJ)) ans = '' L = len(CJ) #print(mn, Cm) for m in itertools.product([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], repeat=missed): CJn = CJ mi = 0 for i in range(len(CJn)): if CJn[i] == '?': CJn = CJn[:i] + str(m[mi]) + CJn[i + 1:] mi += 1 Cn = int(CJn[:L // 2]) Jn = int(CJn[L // 2:]) #print(CJn) if abs(Cn - Jn) < mn: mn = abs(Cn - Jn) Cm = Cn Jm = Jn