Skip to content

Instantly share code, notes, and snippets.

@gorlum0
gorlum0 / A.hs
Created August 19, 2011 03:06
codeforces - 80 - A (hs)
{-# OPTIONS_GHC -O2 -XNoMonomorphismRestriction #-}
{-# LANGUAGE BangPatterns #-}
{-(c) gorlum0 [at] gmail.com-}
isqrt x = truncate (sqrt $ fromIntegral x)
isPrime n
| n`rem`2 == 0 = False
| otherwise = null [x | x <- [3,5.. isqrt n + 1], n`rem`x == 0]
@gorlum0
gorlum0 / A.hs
Created August 23, 2011 13:44
codeforces - 47 - A (hs)
{-# OPTIONS_GHC -O2 -XNoMonomorphismRestriction #-}
{-# LANGUAGE BangPatterns #-}
{-(c) gorlum0 [at] gmail.com-}
import Control.Monad (forM_)
triangular = takeWhile (<=500) $ scanl1 (+) [1..]
main = do
ls <- lines `fmap` getContents
let xs = map read ls
@gorlum0
gorlum0 / B.py
Created August 27, 2011 11:14
codeforces - 1 - B (py)
#!/usr/bin/env python
"""(c) gorlum0 [at] gmail.com"""
import re
import string
from math import log
from sys import stdin
def int2base(x, base, digits = '0123456789abcdefghijklmnopqrstuvwxyz'):
'''inverse of int(x, base), 2 <= base <= 36'''
if x < 0:
@gorlum0
gorlum0 / A.hs
Created August 28, 2011 07:23
codeforces - 2 - A (hs)
{-# OPTIONS_GHC -O2 -XNoMonomorphismRestriction #-}
{-# LANGUAGE BangPatterns #-}
{-(c) gorlum0 [at] gmail.com-}
import qualified Data.Map as M
import qualified Data.Set as S
splitEvery _ [] = []
splitEvery n xs = ys : splitEvery n xs'
where (ys, xs') = splitAt n xs
@gorlum0
gorlum0 / C.hs
Created August 30, 2011 03:55
codeforces - 110 - C (hs)
{-# OPTIONS_GHC -O2 -XNoMonomorphismRestriction #-}
{-# LANGUAGE BangPatterns #-}
{-(c) gorlum0 [at] gmail.com-}
import Data.Array
import Control.Monad (forM_)
maxn = 10^6
dp :: Array Int Int
dp = array bnds [(i, f i) | i <- range bnds]
where
@gorlum0
gorlum0 / C.py
Created August 30, 2011 06:38
codeforces - 110 - C (py)
#!/usr/bin/env python2.6
"""(c) gorlum0 [at] gmail.com"""
from sys import stdin
MAXN = 10**6
dp = [0] * (MAXN+1)
dp[0] = 1
for i in xrange(MAXN+1):
if dp[i-7]:
@gorlum0
gorlum0 / NetworkXOneTimePad.java
Created August 31, 2011 09:27
topcoder - 516-2 - 500 (java)
import java.io.*;
import java.util.*;
import java.math.*;
public class NetworkXOneTimePad
{
Long[] convert(String[] arr) {
int n = arr.length;
Long[] res = new Long[n];
for (int i = 0; i < n; i++)
@gorlum0
gorlum0 / D.py
Created September 4, 2011 07:29
codeforces - 85 - D (py)
#!/usr/bin/env python
"""(c) gorlum0 [at] gmail.com"""
import itertools as it
from sys import stdin
maxn = 10**5
def get_divisors(n, _cache = {}):
if n not in _cache:
divisors = []
for i in xrange(1, int(n**0.5) + 1):
@gorlum0
gorlum0 / CompositeSmash.java
Created September 10, 2011 21:53
tc - 517 div1 - 250 (java)
import java.io.*;
import java.util.*;
import java.math.*;
public class CompositeSmash
{
HashMap<Integer, Boolean> _cache = new HashMap();
boolean possible(int n, int t) {
if (n < t) return false;
if (n == t) return true;
@gorlum0
gorlum0 / NetworkXMessageRecovery.java
Created September 12, 2011 06:28
tc - 516 div2 - 1000 (java)
/*(c) gorlum0 [at] gmail.com*/
import java.io.*;
import java.util.*;
import java.math.*;
public class NetworkXMessageRecovery
{
public String recover(String[] messages) {
StringBuilder res = new StringBuilder();