def solve(s, n): cons = [] last = 0 last_cons = False for c in s: is_cons = c not in 'aeiou' if is_cons: if last_cons: last += 1 else: last = 1 else: last = 0 cons.append(last) last_cons = is_cons L = len(s) v = [0 for _ in xrange(L)] for i in xrange(n - 1, L): is_cons = s[i] not in 'aeiou' v[i] = v[i - 1] if is_cons: if cons[i] > n: v[i] += 1 if cons[i] == n: v[i] = i - n + 2 return sum(v) jam(read, solve)
def rewind(str): return str[1:] + str[0] def generate_all(A, B): pairs = [] for i in xrange(B, A - 1, -1): s = str(i) l = len(s) - 1 for j in xrange(l): s = rewind(s) u = int(s) if u < i and u >= A: pairs.append((i, u)) return set(pairs) def solve(A, B): num = 0 for (i, j) in set_of_everything: if i >= A and i <= B and j >= A and j <= B: num += 1 return num set_of_everything = generate_all(1, 2000000) cj.jam(parse, solve)
#!/usr/bin/env python import cj from itertools import * def read(reader): N = reader / int s = [reader / int for _ in xrange(N)] return N, s def printout(a, b): return '\n' + ' '.join([str(i) for i in a]) + '\n' + ' '.join([str(i) for i in b]) def solve(N, S): sums = {} for mask in product([True, False], repeat=N): a = [x for i, x in enumerate(S) if mask[i]] s = sum(a) if s in sums: return printout(a, sums[s]) else: sums[s] = a return 'Impossible' cj.jam(read, solve)
if x != y: yield y p *= 10 q /= 10 else: break def find(A, B): count = 0 for n in xrange(A, B + 1): for m in rotations(n): if n < m and m <= B: yield n, m pairs = [pair for pair in find(1, 2000000)] def solve(A, B): count = 0 for n, m in pairs: if A <= n and m <= B: count += 1 if n > B: break return count cj.jam(lambda reader: (reader / int, reader / int), solve)