import Data.Char (chr, ord)
import Numeric (showIntAtBase)



quotRemP :: Integral a => a -> a -> (a, a)
quotRemP n d = let (q, r) = quotRem n d
               in if r < 0 then (q+1, r-d) else (q, r)


toNegBase :: Integral a => a -> a -> a
toNegBase b n = let (q, r) = quotRemP n b
                in if q == 0 then r else negate b * toNegBase b q + r



showAtBase :: (Integral a, Show a) => a -> a -> String
showAtBase b n = showIntAtBase (abs b) charOf n ""
  where charOf m | m < 10    = chr $ m + ord '0'
                 | m < 36    = chr $ m + ord 'a' - 10
                 | otherwise = '?'


printAtBase :: (Integral a, Show a) => a -> a -> IO ()
printAtBase b = putStrLn . showAtBase b . toNegBase b

main :: IO ()
main = do
  printAtBase (-2)  10
  printAtBase (-3)  146
  printAtBase (-10) 15
  printAtBase (-16) 107
  printAtBase (-36) 41371458