CSES - Datatähti 2018 alku - Results
Submission details
Task:Bittijono
Sender:FSMnArmosta
Submission time:2017-10-04 17:39:23 +0300
Language:Haskell
Status:READY
Result:22
Feedback
groupverdictscore
#1ACCEPTED7
#2ACCEPTED15
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.04 s1details
#2ACCEPTED0.05 s1details
#3ACCEPTED0.06 s1details
#4ACCEPTED0.05 s1details
#5ACCEPTED0.06 s1details
#6ACCEPTED0.06 s1details
#7ACCEPTED0.06 s1details
#8ACCEPTED0.06 s1details
#9ACCEPTED0.06 s1details
#10ACCEPTED0.05 s1details
#11ACCEPTED0.07 s2details
#12ACCEPTED0.04 s2details
#13ACCEPTED0.04 s2details
#14ACCEPTED0.04 s2details
#15ACCEPTED0.05 s2details
#16ACCEPTED0.07 s2details
#17ACCEPTED0.07 s2details
#18ACCEPTED0.04 s2details
#19ACCEPTED0.07 s2details
#20ACCEPTED0.04 s2details
#21--3details
#22--3details
#23--3details
#24--3details
#25--3details
#26--3details
#27--3details
#28--3details
#29--3details
#30--3details
#31--4details
#32--4details
#33--4details
#34--4details
#35--4details
#36--4details
#37--4details
#38--4details
#39--4details
#40--4details

Compiler report

input/code.hs:3:1: Warning:
    The import of `Data.List' is redundant
      except perhaps to import instances from `Data.List'
    To import instances alone, use: import Data.List()

input/code.hs:6:1: Warning:
    The import of `Data.Fixed' is redundant
      except perhaps to import instances from `Data.Fixed'
    To import instances alone, use: import Data.Fixed()

input/code.hs:7:1: Warning:
    The import of `showHex' from module `Numeric' is redundant

input/code.hs:12:16: Warning:
    Defaulting the following constraint(s) to type `Double'
      (Floating a0) arising from a use of `log' at input/code.hs:12:16-18
      (Fractional a0) arising from a use of `/' at input/code.hs:12:38
      (Num a0)
        arising from a use of `fromIntegral' at input/code.hs:12:21-32
      (RealFrac a0)
        arising from a use of `floor' at input/code.hs:12:8-12
    In the first argument of `(/)', namely `(log (fromIntegral a))'
    In the first argument of `floor', namely
      `((log (fro...

Code

{- Tuli cachetusta koodista vähän likainen olo, varsinkin kun siinä oli ollut se Python. Tässä sama performanssi todellisella Haskellilla ilman esilaskentaa. -}

import Data.List
import Data.Map
import Data.Set
import Data.Fixed
import Numeric (showHex, showIntAtBase)
import Data.Char (intToDigit)

{- integer base 2 logarythm -}
lb :: Int -> Int
lb a = floor ((log (fromIntegral a)) / (log (fromIntegral 2)))

{- Given the cache, look up the combinations for a given element -}

countCombinations cache index = sum (Prelude.map (length) hit)
    where hit = snd (Data.Map.elemAt (Data.Map.findIndex index cache) cache)



{- Get the set for length 1 elements in a given number -}

basePairs 0 = Data.Set.fromList [0]
basePairs bitstring = zeroElem `Data.Set.union` oneElem

    {- If all the bits of the bitstring are on, the bitstring + 1 is a power of two => if the bitstring + 1 is a power of two, then there are no zeroes in the bitstring -}

    where zeroElem = if 2^(lb (bitstring + 1))/= bitstring + 1 then Data.Set.fromList [0] else Data.Set.empty

    {- If the bistring is non-zero, it has at least one bit on -}

          oneElem = if bitstring == 0 then Data.Set.empty else Data.Set.fromList [1]




{- Find the union of the new combinations brought by the addition of the new possible bit and the combinations alreeady known -}

newCombinations previous current newBit = current `Data.Set.union` new
    where new = Data.Set.map (computeCombination) previous
          computeCombination base = 2*base + newBit



{- Construct a chain from an ancestors -}


fromAncestor (first:second:ancestors) newBit = (newCombinations first second newBit):(fromAncestor (second:ancestors) newBit)
fromAncestor (ancestor:[]) newBit = [newCombinations ancestor Data.Set.empty newBit]


{- Compute the combinations for a bitstring with a known ancestor -}

iterateOnce 1 ancestor = [(Data.Set.fromList [1])]
iterateOnce current ancestor = ((basePairs current):(fromAncestor ancestor newBit))
    where newBit = current `rem` 2

{- Compute the combinations from the new number alone -}

body 1 cache = ((iterateOnce 1 []), cache)
body bitstring cache = if Data.Map.member bitstring cache then (snd (Data.Map.elemAt (Data.Map.findIndex bitstring cache) cache), cache) else ((iterateOnce bitstring ancestor), resultCache)
    where (ancestor, returnedCache) = body (bitstring `div` 2) cache
          newCache = (returnedCache `Data.Map.union` cache)
          result = iterateOnce bitstring ancestor
          resultCache = newCache `Data.Map.union` (Data.Map.fromList [(bitstring, result)])

{- Compute the number of combinations for a number -}

combinationCount bitstring cache = (sum (Prelude.map (Data.Set.size) construct), newCache)
    where (construct, newCache) = body bitstring cache

{- Compute the starting point with a given target -}


startingPoint current target cache = if base < target then (if combinationsA > combinationsB then startingPoint a target newCache else startingPoint b target newCache) else (lb current, newCache)
    where a = current * 2
          b = a + 1
          (base, cache2) = combinationCount current cache
          (combinationsA, cacheA) = combinationCount a cache2
          (combinationsB, newCache) = combinationCount b cacheA

{- Compute the smallest number to have a given number of combinations -}

solution target = testForwards (2^start) target cache2
    where (start, cache2) = startingPoint 1 target (Data.Map.fromList [])
          testForwards i target cache = if combinations == target then i else testForwards (i+1) target nextCache
            where (combinations, nextCache) = combinationCount i cache

main :: IO ()
main = do
    num <- getLine
    let target = read num :: Int
    putStrLn (showIntAtBase 2 intToDigit (solution target) "")

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
1

correct output
1

user output
1

Test 2

Group: 1

Verdict: ACCEPTED

input
2

correct output
11

user output
11

Test 3

Group: 1

Verdict: ACCEPTED

input
3

correct output
10

user output
10

Test 4

Group: 1

Verdict: ACCEPTED

input
4

correct output
1111

user output
1111

Test 5

Group: 1

Verdict: ACCEPTED

input
5

correct output
110

user output
100

Test 6

Group: 1

Verdict: ACCEPTED

input
6

correct output
101

user output
101

Test 7

Group: 1

Verdict: ACCEPTED

input
7

correct output
1110

user output
1000

Test 8

Group: 1

Verdict: ACCEPTED

input
8

correct output
1100

user output
1100

Test 9

Group: 1

Verdict: ACCEPTED

input
9

correct output
1101

user output
1011

Test 10

Group: 1

Verdict: ACCEPTED

input
10

correct output
1001

user output
1001

Test 11

Group: 2

Verdict: ACCEPTED

input
38

correct output
1101011

user output
1101011

Test 12

Group: 2

Verdict: ACCEPTED

input
13

correct output
11011

user output
11011

Test 13

Group: 2

Verdict: ACCEPTED

input
90

correct output
111001010

user output
100100010

Test 14

Group: 2

Verdict: ACCEPTED

input
25

correct output
110010

user output
101100

Test 15

Group: 2

Verdict: ACCEPTED

input
82

correct output
111001101

user output
100010011

Test 16

Group: 2

Verdict: ACCEPTED

input
94

correct output
1100011110

user output
1000011100

Test 17

Group: 2

Verdict: ACCEPTED

input
100

correct output
1111001001

user output
1001001111

Test 18

Group: 2

Verdict: ACCEPTED

input
99

correct output
110010010

user output
100011010

Test 19

Group: 2

Verdict: ACCEPTED

input
98

correct output
110110010

user output
100111010

Test 20

Group: 2

Verdict: ACCEPTED

input
92

correct output
100110001

user output
100011001

Test 21

Group: 3

Verdict:

input
1666

correct output
101101100100101

user output
(empty)

Test 22

Group: 3

Verdict:

input
897

correct output
11101001101010

user output
(empty)

Test 23

Group: 3

Verdict:

input
4466

correct output
111101010110100101

user output
(empty)

Test 24

Group: 3

Verdict:

input
4240

correct output
11011001011010101

user output
(empty)

Test 25

Group: 3

Verdict:

input
3089

correct output
1011001010100101

user output
(empty)

Test 26

Group: 3

Verdict:

input
4697

correct output
11010101101010110

user output
(empty)

Test 27

Group: 3

Verdict:

input
4608

correct output
11010110101001010

user output
(empty)

Test 28

Group: 3

Verdict:

input
4625

correct output
111011001100101001

user output
(empty)

Test 29

Group: 3

Verdict:

input
4611

correct output
11010101010101100

user output
(empty)

Test 30

Group: 3

Verdict:

input
4917

correct output
10110100101010110

user output
(empty)

Test 31

Group: 4

Verdict:

input
178555

correct output
1011010110110101010110110

user output
(empty)

Test 32

Group: 4

Verdict:

input
864856

correct output
10111010110110100100101010010

user output
(empty)

Test 33

Group: 4

Verdict:

input
112146

correct output
1101110101011001100100110

user output
(empty)

Test 34

Group: 4

Verdict:

input
741124

correct output
1011010011010101100101011010

user output
(empty)

Test 35

Group: 4

Verdict:

input
511902

correct output
1011010100011010100101001110

user output
(empty)

Test 36

Group: 4

Verdict:

input
920019

correct output
11100100101101010101001101010

user output
(empty)

Test 37

Group: 4

Verdict:

input
933943

correct output
10101011010100100110100111001

user output
(empty)

Test 38

Group: 4

Verdict:

input
973410

correct output
1011010101011010101010101001

user output
(empty)

Test 39

Group: 4

Verdict:

input
954943

correct output
10110110010011010100100110101

user output
(empty)

Test 40

Group: 4

Verdict:

input
911674

correct output
1010110010110101010101010110

user output
(empty)