Ejemplo n.º 1
0
def test():
    iline()
    M = iline()

    def solve():
        diffs = [max(0, a - b) for a, b in izip(M[:-1], M[1:])]
        A = sum(diffs)
        rate = max(diffs)
        B = sum(min(a, rate) for a in M[:-1])
        print A, B

    return solve
Ejemplo n.º 2
0
def test():
    N, J = iline()
    
    yield
    
    print
    i = 0
    while J:
      seq = '1' + (('0'*N)+bin(i)[2:])[-(N-2):] + '1'
      divisors = []
      for base in xrange(2,11):
        val = int(seq, base)
        p = 2
        while p*p <= 10000000:
          if val % p == 0:
            divisors.append(p)
            val = None
            break
          p += 1
            
        if val is not None:
           seq = None
           break
      if seq is not None:
        print >>sys.stderr, 'Found one,', J, 'missing'
        print seq, ' '.join(map(str, divisors))
        J -= 1
        
      i += 1
Ejemplo n.º 3
0
def test():
    R, C = iline()
    world = [raw_input() for y in xrange(R)]

    yield

    n = 0
    for y in xrange(R):
        for x in xrange(C):
            if world[y][x] != '.':

                dx, dy = DIRS[world[y][x]]

                def path_ok(dx, dy):
                    _x = x + dx
                    _y = y + dy
                    if _y < 0 or _x < 0 or _y >= R or _x >= C: return False
                    while world[_y][_x] == '.':
                        _x += dx
                        _y += dy
                        if _y < 0 or _x < 0 or _y >= R or _x >= C: return False

                    return True

                if not path_ok(dx, dy):
                    n += 1
                    if not any(path_ok(dx, dy) for dx, dy in DIRS.values()):
                        print 'IMPOSSIBLE'
                        return

    print n
Ejemplo n.º 4
0
def test():
    A, B, C, K = iline()
    
    yield
    
    triples = set()
    pairs = defaultdict(int)
    
    while True:
        n = len(triples)
        for a in xrange(A):
            a = (a+len(triples))%A
            for b in xrange(B):
                b = (b+len(triples))%B
                for c in xrange(C):
                    c = (c+len(triples))%C
                    
                    if (a,b,c) in triples or pairs[('ab',a,b)] >= K or pairs[('ac',a,c)] >= K or pairs[('bc',b,c)] >= K:
                        continue
                        
                    triples.add((a,b,c))
                    pairs[('ab',a,b)] += 1
                    pairs[('ac',a,c)] += 1
                    pairs[('bc',b,c)] += 1
    
        if len(triples) == n:
            break
            
    print len(triples)
    for a, b, c in triples:
        print a+1, b+1, c+1
Ejemplo n.º 5
0
def test():
    N, r, p, s = iline()

    yield

    for win in 'PRS':
        x = win
        for i in xrange(N):
            x = ''.join(map(translate.get, x))

        if x.count('R') != r or x.count('S') != s or x.count('P') != p:
            continue

        answer = list(x)
        for i in range(1, N + 1):
            length = 2**i
            for a in range(0, 2**N, length):
                b = a + length / 2
                c = b + length / 2
                if answer[a:b] > answer[b:c]:
                    tmp = answer[a:b]
                    answer[a:b] = answer[b:c]
                    answer[b:c] = tmp
        print ''.join(answer)
        return

    print 'IMPOSSIBLE'
Ejemplo n.º 6
0
def test():
    L, X = iline()
    S, = line()

    def solve():
        now = '1'
        compressed = reduce(mul, S, '1')

        now = '1'
        i = 0
        need = 'i', 'j', 'k'
        for L in repeat(S, X):
            if need:
                for c in S:
                    now = mul(now, c)
                    i += 1
                    if need and i > L:
                        print 'NO'
                        return

                    if need and now == need[0]:
                        need = need[1:]
                        now = '1'
                        i = 0
            else:
                now = mul(now, compressed)

        if need or now != '1':
            print 'NO'
        else:
            print 'YES'

    return solve
Ejemplo n.º 7
0
def test():
    n, k = iline()
    sizes = [iline() for i in xrange(n)]

    yield

    sizes.sort(key=lambda (r, h): r * h)

    maybe = sizes[:-k + 1] if k > 1 else sizes
    certain = sizes[-k + 1:] if k > 1 else []

    certain_r = max(r for r, h in certain) if certain else 0
    certain_area = sum(2 * r * h for r, h in certain) + (certain_r**2)

    maybe_best = max(2 * r * h + max(0, (r**2) - (certain_r**2))
                     for r, h in maybe)

    print '{:.9f}'.format(pi * (certain_area + maybe_best))
Ejemplo n.º 8
0
def test():
    iline()
    nums = iline()

    yield

    total = sum(nums)
    nums = [[count, chr(ord('A') + i), i] for i, count in enumerate(nums)]

    answer = ''
    while total > 0:
        count, c, i = max(nums)
        answer += c
        nums[i][0] -= 1
        total -= 1

        if total % 2 == 0:
            answer += ' '
    print answer
Ejemplo n.º 9
0
Archivo: 5.py Proyecto: qifanyyy/CLCDSA
def test():
    N, = iline()
    P = [iline() for i in xrange(N)]

    def solve():
        print
        for px, py in P:
            Q = [(atan2(qy - py, qx - px), (qx - px, qy - py)) for qx, qy in P
                 if px != qx or py != qy]
            N = len(Q)
            Q.sort()
            Q.extend([(q + 2 * pi, pos) for q, pos in Q])
            a = 0
            ans = N
            for b in xrange(N, len(Q)):
                a = max(a, b - N)
                while a < b and Q[b][1][0] * Q[a][1][1] - Q[b][1][1] * Q[a][1][
                        0] >= 0:
                    a += 1
                ans = min(ans, b - a)

            print ans

    return solve
Ejemplo n.º 10
0
def test():
    K, C, S = iline()
    
    yield
    
    if C*S < K:
        print 'IMPOSSIBLE'
    else:
        for i in xrange(0, K, C):
            n = 0
            for j in xrange(C):
                n *= K
                if i+j < K:
                    n += i+j
            print n+1,
        print
Ejemplo n.º 11
0
def test():
    N, = iline()
    sentences = [set(line()) for i in xrange(N)]

    yield

    answer = 1e100
    for bits in xrange(1 << (N - 2)):
        words = [set(), set()]
        bits = 4 * bits + 2
        for i, s in enumerate(sentences):
            language = 1 if (bits & (1 << i)) else 0
            words[language] |= s

        answer = min(answer, len(words[0] & words[1]))

    print answer
Ejemplo n.º 12
0
def test():
    B, M = iline()

    yield

    S = 2**(B - 2)

    if S < M:
        print 'IMPOSSIBLE'
        return

    bonus = (M == S)
    if bonus:
        M = S - 1

    print 'POSSIBLE'
    print('0' * B + bin(M)[2:] + ('1' if bonus else '0'))[-B:]
    for i in xrange(2, B + 1):
        print '0' * i + '1' * (B - i)
Ejemplo n.º 13
0
def test():
    N, K = iline()
    P_ = fline()

    yield

    P_.sort()
    answer = 0.0
    for n in xrange(K + 1):
        P = P_[:n] + (P_[-K + n:] if n != K else [])
        #print >>stderr, P
        F = {0: 1.0}
        for p in P:
            score = lambda p, x: p * F.get(x - 1, 0.0) + (1 - p) * F.get(
                x, 0.0)
            F = {x: score(p, x) for x in xrange(N)}

        answer = max(answer, F[K / 2])

    print '{:.12f}'.format(answer)
Ejemplo n.º 14
0
def test():
    n, = iline()
    topics = { tuple(line()) for i in xrange(n) }
    
    yield
    
    first = { a for a,b in topics }
    second = { b for a,b in topics }
    
    answer = 0
    
    for mask in xrange(2**n):
        mask = ('0'*n + bin(mask)[2:])[-n:]
        
        first_ = { a for (a,b), m in zip(topics, mask) if m == '1' }
        second_ = { b for (a,b), m in zip(topics, mask) if m == '1' }
        
        if first_ == first and second_ == second:
            answer = max(answer, mask.count('0'))
            
    print answer
Ejemplo n.º 15
0
def test():
    n, k = iline()

    yield

    values = [(n, 1)]
    while True:
        values = sorted(values)[::-1]

        new_values = defaultdict(int)
        for hole, spots in values:
            a = (hole - 1) / 2
            b = (hole - 1) - a

            if k <= spots:
                print max(a, b), min(a, b)
                return

            k -= spots

            new_values[a] += spots
            new_values[b] += spots

        values = new_values.items()
Ejemplo n.º 16
0
        need = 'i', 'j', 'k'
        for L in repeat(S, X):
            if need:
                for c in S:
                    now = mul(now, c)
                    i += 1
                    if need and i > L:
                        print 'NO'
                        return

                    if need and now == need[0]:
                        need = need[1:]
                        now = '1'
                        i = 0
            else:
                now = mul(now, compressed)

        if need or now != '1':
            print 'NO'
        else:
            print 'YES'

    return solve


if __name__ == '__main__':
    T, = iline()
    for i in xrange(1, T + 1):
        print 'Case #%d:' % i,
        test()
Ejemplo n.º 17
0
def test():
    C, J = iline()
    cjobs = [iline() for i in xrange(C)]
    jjobs = [iline() for i in xrange(J)]

    yield

    jobs = [(c, d, 'c') for c, d in cjobs] + [(c, d, 'j') for c, d in jjobs]
    jobs.sort()

    def solve(who, need, answer):
        gains = []

        for prev, next in zip(jobs, jobs[1:] + jobs[:1]):
            if prev[2] == next[2]:
                gains.append((next[0] - prev[1] + FULL) % FULL)

        gains.sort()
        #print >>stderr, need, gains

        while need > 0:
            answer += 2
            gain = gains[-1]
            gains.pop()
            need -= gain

        print answer

    if C == 0:
        solve('c', HALF, 0)
        return
    elif J == 0:
        solve('j', HALF, 0)
        return

    i = 0
    while jobs[i][2] == jobs[-1][2]:
        jobs[i] = (jobs[i][0] + FULL, jobs[i][1] + FULL, jobs[i][2])
        i += 1

    jobs = jobs[i:] + jobs[:i]

    def merge():
        left = 0
        while left < len(jobs):
            right = left
            while right < len(jobs) and jobs[right][2] == jobs[left][2]:
                right += 1
            yield jobs[left][0], jobs[right - 1][1], jobs[left][2]
            left = right

    def maximize(jobs, who):

        other = {'c': 'j', 'j': 'c'}
        change_min = jobs[-1][1] - FULL

        for job in jobs:
            change_max = job[0]

            if job[2] == who:
                yield change_max, other[job[2]]
            else:
                yield change_min, other[job[2]]

            change_min = job[1]

    mjobs = list(merge())
    cchanges = list(maximize(mjobs, 'c'))
    cchanges.append((cchanges[0][0] + FULL, cchanges[0][1]))
    jchanges = list(maximize(mjobs, 'j'))
    jchanges.append((jchanges[0][0] + FULL, jchanges[0][1]))

    ctotal = sum((d - c)
                 for (c, who), (d, _) in zip(cchanges[:-1], cchanges[1:])
                 if who == 'c')
    jtotal = sum((d - c)
                 for (c, who), (d, _) in zip(jchanges[:-1], jchanges[1:])
                 if who == 'j')

    #print >>stderr, jobs
    #print >>stderr, mjobs
    #print >>stderr, cchanges
    #print >>stderr, jchanges
    #print >>stderr, ctotal, jtotal

    if ctotal < HALF:
        solve('c', HALF - ctotal, len(cchanges) - 1)
    elif jtotal < HALF:
        solve('j', HALF - jtotal, len(cchanges) - 1)
    else:
        print len(cchanges) - 1