Project Euler #121: Disc game prize fund

Sort by

recency

|

11 Discussions

|

  • + 2 comments

    I had the correct algorithm, but was getting failure on test 3. Now that I fixed the problem, I realize that the issue was all due to not handling the integer division with enough precision for the larger N values. To get around that, I used the python fraction library. Here's the code:

    from fractions import Fraction
    from math import floor
    
    def compute_outcomes(n):
        outcomes = [0]*(n+1)
        outcomes[-1] = 1
        outcomes[-2] = 1 #outcomes represents values of outcome tree after i turns
                             
        for i in range(2, n+1): #looping through levels i of tree
            for j in range(n):
                outcomes[j] = outcomes[j + 1] #number of ways to get to given node is above right node plus i*(above left node)               
    
            outcomes[n] = 0 #far right node initialized to 0 since only contribution is from above left node
    
            for j in range(n, 0, -1): #not including node 0 since it doesn't have contribution from above left
                outcomes[j] += outcomes[j - 1] * i; #contribution from above left node of tree
                
        return outcomes
    
    def compute_price(outcomes):
        n = len(outcomes) - 1
        k = (n+1)//2
        return Fraction(sum(outcomes), sum(outcomes[:k]))
    
    
    T = int(input())
    for j in range(T):
        N = int(input())
        outcomes = compute_outcomes(N)
        s = compute_price(outcomes)
        print(floor(s))
    
  • + 0 comments

    for 5 turns..what will be the probability of winning ??

  • + 1 comment

    i have my case 2terminated due to time

  • + 0 comments

    I have my test #2 and #3 wrong, but I am almost sure than I am doing everything correctly.

    I even checked the int overflow (2^32) and change it to BigDecimal, remove the scientific notation to display out as "single integer".

    Can someone check my result for the first N 10 cases? 1 = 1 (2/1=2 this would be no loss no gain,so -1), 2 = 5 (6/1=6 this would be no loss no gain,so -1), 3 = 3 (24/7), 4 = 10 (120/11), 5 = 7 (720/101), 6 = 25 (5040/197), 7 = 17 (40320/2311), 8 = 70 (362880/5119), 9 = 49 (3628800/73639), 10 = 225 (39916800/177299),

    Still can't figure out what i am doing wrong or how to debug it. I would love some guidance, thanks in advanced :)

  • + 0 comments

    I think that for finding the prize we need to devide denominator by numerator. But i got my test 2 and 3 wrong.