def solve(n=10000019, c=100, mod=10**9):
    cap = 2 * c
    if n % 9 > 2:
        print("Error: Unequal number of possible values in tiers 1, 2, and 3")
        return -1
    low_choices = 2 * (n // 9) + (n % 9)
    mid_choices = 2 * (n // 9)
    high_choices = n // 9

    low_mults = [pow(low_choices, x, mod) % mod for x in range(cap + 1)]
    mid_mults = [ [ (choose(y, x) * pow(mid_choices, x, mod)) % mod         \
      for x in range(y + 1)] for y in range(cap + 1)]
    high_mults = [ (choose(cap, x) * pow(high_choices, x, mod)) % mod       \
      for x in range(cap + 1)]

    losing_positions = 0
    for h in range(0, cap + 1, 2):
        for m1 in range(0, cap - h + 1):
            for m2 in range(m1 % 2, m1 + 1, 2):
                for m3 in range(m1 % 2, min(m2 + 1, cap - h - m1 - m2 + 1), 2):
                    term = high_mults[h]
                    remaining = cap - h
                    for m in [m1, m2, m3]:
                        term *= mid_mults[remaining][m]
                        remaining -= m
                    term *= low_mults[remaining]
                    if len(set([m1, m2, m3])) == 3:\
                        term *= 6
                    elif len(set([m1, m2, m3])) == 2:
                        term *= 3
                    losing_positions += term
                    losing_positions %= mod
    return (pow(n, cap, mod) - losing_positions) % mod
Exemple #2
0
def solve():
    start = time()
    oneBits = [0 for x in range(33)]
    oneBits[0] = 1
    totalFinishes = 0
    weightedFinishes = 0
    turn = 1
    oldResult = 0
    newResult = 3
    while oldResult != newResult:
        newOneBits = [0 for x in range(33)]
        for startBits in range(len(oneBits)):
            for endBits in range(startBits, len(oneBits)):
                newInstances = choose(32 - startBits, endBits - startBits)
                newOneBits[endBits] += oneBits[
                    startBits] * newInstances * 2**startBits
        totalFinishes *= 2**32
        weightedFinishes *= 2**32
        totalFinishes += newOneBits[-1]
        weightedFinishes += turn * newOneBits[-1]
        newOneBits[-1] = 0
        oneBits = newOneBits
        turn += 1
        oldResult = newResult
        newResult = round(weightedFinishes / totalFinishes, 13)
    result = newResult
    result *= 10**10
    result = round(result, 0)
    result /= 10**10
    peresult(323, result, time() - start)
def solve(cap = 10 ** 16):
    primes = primesbelow(100)
    
    mat = [ [0 for col in range(len(primes)-2)] for row in range(len(primes)-3)]
    for row in range(len(primes) - 3):
        for col in range(row + 1):
            mat[row][col] = choose(row + 4, col + 4)
        mat[row][-1] = 1
    multipliers = matsolve(mat)
    multipliers = list(map(int, multipliers))

    result = 0
    for length in range(4, len(primes)):
        indices = [i for i in range(length)]
        while True:
            term = (cap - 1) // (subproduct(primes, indices))
            result += multipliers[length - 4] * term
            # Move the rightmost index that we can right.
            # (except for the absolute rightmost index
            # if the term is zero; that would be pointless)
            if term != 0 and indices[-1] != len(primes) - 1:
                indices[-1] += 1
                continue
            for i in range(len(indices) - 2, -1, -1):
                if indices[i] + 1 != indices[i + 1]:
                    for j in range(len(indices) - 1, i - 1, -1):
                        indices[j] = indices[i] + j - i + 1
                    break
            else:
                # We've found all we can for this index count
                break
    return result
def solve():
    start = time()
    live, dead = 21, 75
    for turn in range(20, -1, -1):
        live, dead = (live + dead) * turn, 76 * live + 75 * dead
    result = dead * factorial(75) * choose(25, 3) / factorial(100)
    result *= 10**13
    result = round(int(result), -1)
    result /= 10**13
    peresult(239, result, time() - start)
Exemple #5
0
def solve():
        start = time()
        totalInsts = 0
        goodInsts = 0
        for tossesWon in range(1001):
                newInsts = choose(1000, tossesWon)
                totalInsts += newInsts
                if tossesWon > 333:
                        goodInsts += newInsts
        for tossesWon in range(334, 1001):
                bestBet = (3 * tossesWon - 1000) / 2000 #Hand-computed formula
                maxWinnings = (1 + 2 * bestBet) ** tossesWon * (1 - bestBet) ** (1000 - tossesWon)
                if maxWinnings < 10 ** 9:
                        goodInsts -= choose(1000, tossesWon)
                else:
                        break
        result = goodInsts / totalInsts
        result *= 10 ** 13
        result = round(result, -1)
        result /= 10 ** 13
        peresult(267, result, time() - start)
def solve():
    start = time()
    chose = [[choose(x, y) for y in range(x + 1)] for x in range(25)]
    pOfN = [0 for x in range(27)]
    for p1 in range(25):
        for p2 in range(p1 + 1, 26):
            for countA in range(p1 + 1):
                probA = chose[p1][countA]
                for countB in range(p2 - p1):
                    probB = chose[p2 - p1 - 1][countB] * (2**countB)
                    for countC in range(26 - p2):
                        probC = chose[25 - p2][countC]
                        pOfN[countA + countB + countC] += probA * probB * probC
    print(pOfN)
    peresult(158, max(pOfN), time() - start)
    # I'm using mousehead's Song Lyrics dataset, you can get it from here:
    # https://www.kaggle.com/mousehead/songlyrics/home

    # change this so it points to where you downloaded your dataset...
    dataPath = "D:\\Data\\songdata.csv"

    # select the Artist you want...
    artist = "ABBA"
    print("Generating a new {} song!".format(artist))

    # create the initial distribution and the transition matrix from the data
    initialDistr, transitionMatrix = createMarkovChain(dataPath, artist)

    # first, we decide the initial state
    sequence = [choose(initialDistr)]

    # then, we'll create the rest of the song, up to 500 words...
    for x in range(500):
        # find the probabilities for the next state
        state = sequence[-1]

        # if the word isn't in the transitionMatrix we can't continue
        if state not in transitionMatrix:
            break

        # grab the next distribution
        nextDistr = transitionMatrix[state]

        # then decide the outcome
        sequence.append(choose(nextDistr))
Exemple #8
0
def solve():
    start = time()
    result = 1  # 2^30 itself (all other solutions are 30-bit)
    for x in range(1, 16):
        result += choose(31 - x, x)
    peresult(301, result, time() - start)
Exemple #9
0
def solve(x=20, y=20):
    return choose(x + y, x)