This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
def _calc_size(tree, root, found, sizes): | |
"""Practically a dfs on the remaining nodes | |
where the size of the parent node is the sum of | |
sizes of the child nodes.""" | |
stack = [(root, 0)] | |
on_route = set([root]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from heapq import heappop as pop, heappush as push | |
from math import floor, ceil | |
t = int(raw_input().strip()) | |
def show(i, sol): | |
print "Case #%s: %s" % (i, sol) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt, ceil | |
LIMIT = 10 ** 5 | |
n = int(raw_input().strip()) | |
SLIMIT = int(ceil(sqrt(LIMIT))) | |
small = [[0] * (SLIMIT + 1) for _ in xrange(SLIMIT + 1)] | |
big = [0 for _ in xrange(LIMIT + 1)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
def show(i, key, count): | |
print "Case #%s: %s %s" % (i, key, count) | |
t = int(raw_input().strip()) | |
for ct in xrange(1, t + 1): | |
s = int(raw_input().strip()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t = int(raw_input().strip()) | |
M, S, P = range(3) | |
def show(case, sol): | |
print "Case #%s: %s" % (case, sol) | |
def transform(m, s, p, limit): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import choice | |
import sys | |
t = int(raw_input().strip()) | |
LOW, HIGH = 0.005, 0.1 | |
def calc(p, count, curr, left): | |
return pow(p, count) * pow(1.0 - p, curr - count) * pow(1.0 - p, left) | |
for case in xrange(1, t + 1): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
def fill(distances, was, backlinks, top, curr, dist): | |
distances[curr] = dist | |
was[curr] = True | |
while dist > 0: | |
distances[curr] = dist | |
was[curr] = True | |
curr = backlinks[curr][0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
# 2SAT linear solver START | |
def strongly_connected(graph): | |
""" From Cormen: Strongly connected components chapter. """ | |
l = len(graph) | |
normal = [list(graph[i]) for i in xrange(l)] | |
reverse = [[] for _ in xrange(l)] | |
for fr, tos in enumerate(normal): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
from itertools import product | |
# 2SAT linear solver START | |
def strongly_connected(graph): | |
""" From Cormen: Strongly connected components chapter. """ | |
l = len(graph) | |
normal = [list(graph[i]) for i in xrange(l)] | |
reverse = [[] for _ in xrange(l)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MOD = 10 ** 9 + 7 | |
LEFT, RIGHT = range(2) | |
def modpow(x, y, mod): | |
elems = [] | |
while y: | |
elems.append(y % 2) | |
y >>= 1 | |
el = len(elems) |