Ejemplo n.º 1
0
def lexi_after(alpha, n):
    count = 0
    for i in range(alpha):
        for j in range(i + 1, alpha):
            for a, b, c in pick_n((i, j - i - 1, alpha - j - 1), n - 2):
                count += nCr(i, a) * nCr(j - i - 1, b) * 2**b * nCr(
                    alpha - j - 1, c)
    return count
Ejemplo n.º 2
0
def main():
    size = 250250
    div = 250
    mod = 10 ** 16

    remainder = [0] * div
    cache = [[0] * div for _ in range(div)]
    # cache = np.zeros((div, div), dtype=np.int64)

    for i in range(1, size + 1):
        remainder[0 if i % 10 == 0 else pow(i, i, 250)] += 1
    # print(remainder)

    cache[0][0] = pow(2, remainder[0], mod) - 1
    for rem in range(1, div):
        print(rem)

        change = [0] * div
        for c in range(remainder[rem] + 1):
            change[rem * c % div] = (change[rem * c % div] + nCr(remainder[rem], c)) % mod
        # print(change)

        for index, i in enumerate(change):
            for j in range(div):
                new = cache[rem][(index + j) % div] + i * cache[rem - 1][j]
                cache[rem][(index + j) % div] = new % mod
        # print(rem, cache[rem])

    print(cache[-1][0] - 1)
Ejemplo n.º 3
0
def main():
    size = 250250
    div = 250
    mod = 10 ** 16

    remainder = [0] * div
    cache = [[0] * div for _ in range(div)]
    # cache = np.zeros((div, div), dtype=np.int64)

    for i in range(1, size + 1):
        remainder[0 if i % 10 == 0 else pow(i, i, 250)] += 1
    # print(remainder)

    cache[0][0] = pow(2, remainder[0], mod) - 1
    for rem in range(1, div):
        print(rem)

        change = [0] * div
        for c in range(remainder[rem]+1):
            change[rem * c % div] = (change[rem * c % div] + nCr(remainder[rem], c)) % mod
        # print(change)

        for index, i in enumerate(change):
            for j in range(div):
                new = cache [rem] [(index+j)%div] + i * cache [rem-1] [j]
                cache [rem] [(index+j)%div] = new % mod
        # print(rem, cache[rem])

    print(cache[-1][0] - 1)
Ejemplo n.º 4
0
def partition_maxlen(n, maxlen):
    total = 0
    for length, count in partition_length(n).items():
        if length > maxlen:
            continue
        total += count * nCr(maxlen, length)
    return total
Ejemplo n.º 5
0
def main():
    n = 12

    count = 0
    for i in range(2, n//2+1):
        this_count = check(i)

        this_count *= nCr(n, 2*i)
        count += this_count

    print(count)
Ejemplo n.º 6
0
def main():
    n = 12

    count = 0
    for i in range(2, n // 2 + 1):
        this_count = check(i)

        this_count *= nCr(n, 2 * i)
        count += this_count

    print(count)
Ejemplo n.º 7
0
def main():
    """
    def get_digits(n):
        return list(map(int, str(n)))
    same = 0
    increasing = 0
    decreasing = 0
    for i in range(1, 10 ** 6):
        all_digits = get_digits(i)

        if all([j == all_digits[0] for j in all_digits]):
            same += 1
            continue

        all_increasing = all(all_digits[j-1] <= all_digits[j] for j in range(1, len(all_digits)))
        if all_increasing:
            increasing += 1
            continue

        all_decreasing = all(all_digits[j-1] >= all_digits[j] for j in range(1, len(all_digits)))
        if all_decreasing:
            decreasing += 1

    count = same + increasing + decreasing
    print(same, increasing, decreasing)
    print(count, 10**6-1 - count)
    # experiments finished
    """

    digits = 100

    increase_cache = np.zeros((10, digits+1), dtype=np.int64)  # 1~9  increase_cache[a, b] --> all digits <= a; b digits
    increase_cache[1:, 0] = [1] * 9
    # print(increase_cache)
    for i in range(1, digits+1):
        for j in range(1, 10):
            increase_cache[j, i] = increase_cache[j-1, i] + increase_cache[j, i-1]
    increase = sum(increase_cache[9, i] for i in range(1, digits+1))
    # print(increase_cache)
    print(increase)

    decrease_cache = np.zeros((11, digits+1), dtype=np.int64)  # 9~0  decrease_cache[a, b] --> all digits >= a; b digits
    decrease_cache[1:10, 0] = [1] * 9
    # print(decrease_cache)
    for i in range(1, digits+1):
        for j in range(9, -1, -1):
            decrease_cache[j, i] = decrease_cache[j+1, i] + decrease_cache[j, i-1]
    decrease = sum(decrease_cache[0, i] for i in range(1, digits+1))
    # print(decrease_cache)
    print(decrease)

    result = increase + decrease - 9 * digits  # 11111111...  ~  99999999...
    print(result)

    print("\nAnother extremely elegant solution:")
    count = 0
    for i in range(1, 101):
        count += nCr(i+8, i)  # increase up to 8 times
        count += nCr(i+9, i)  # decrease up to 9 times
        count -= 10
    print(count)
Ejemplo n.º 8
0
def main():
    """
    def get_digits(n):
        return list(map(int, str(n)))
    same = 0
    increasing = 0
    decreasing = 0
    for i in range(1, 10 ** 6):
        all_digits = get_digits(i)

        if all([j == all_digits[0] for j in all_digits]):
            same += 1
            continue

        all_increasing = all(all_digits[j-1] <= all_digits[j] for j in range(1, len(all_digits)))
        if all_increasing:
            increasing += 1
            continue

        all_decreasing = all(all_digits[j-1] >= all_digits[j] for j in range(1, len(all_digits)))
        if all_decreasing:
            decreasing += 1

    count = same + increasing + decreasing
    print(same, increasing, decreasing)
    print(count, 10**6-1 - count)
    # experiments finished
    """

    digits = 100

    increase_cache = np.zeros(
        (10, digits + 1), dtype=np.int64
    )  # 1~9  increase_cache[a, b] --> all digits <= a; b digits
    increase_cache[1:, 0] = [1] * 9
    # print(increase_cache)
    for i in range(1, digits + 1):
        for j in range(1, 10):
            increase_cache[j, i] = increase_cache[j - 1,
                                                  i] + increase_cache[j, i - 1]
    increase = sum(increase_cache[9, i] for i in range(1, digits + 1))
    # print(increase_cache)
    print(increase)

    decrease_cache = np.zeros(
        (11, digits + 1), dtype=np.int64
    )  # 9~0  decrease_cache[a, b] --> all digits >= a; b digits
    decrease_cache[1:10, 0] = [1] * 9
    # print(decrease_cache)
    for i in range(1, digits + 1):
        for j in range(9, -1, -1):
            decrease_cache[j, i] = decrease_cache[j + 1,
                                                  i] + decrease_cache[j, i - 1]
    decrease = sum(decrease_cache[0, i] for i in range(1, digits + 1))
    # print(decrease_cache)
    print(decrease)

    result = increase + decrease - 9 * digits  # 11111111...  ~  99999999...
    print(result)

    print("\nAnother extremely elegant solution:")
    count = 0
    for i in range(1, 101):
        count += nCr(i + 8, i)  # increase up to 8 times
        count += nCr(i + 9, i)  # decrease up to 9 times
        count -= 10
    print(count)