888. 求组合数 IV

摘要
Title: 888. 求组合数 IV
Tag: 组合数
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

888. 求组合数 IV

  • 题意

    输入 a,b,求 C(a, b) 的值。
    注意结果可能很大,需要使用高精度计算。

  • 思路

    根据定义的求法
    img


    直接用python即可

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    '''
    Author: NEFU AB-IN
    Date: 2022-03-12 12:34:28
    FilePath: \ACM\Acwing\888.py
    LastEditTime: 2022-03-12 12:34:29
    '''


    def C(a, b):
    i, j, res = a, 1, 1
    while j <= b:
    res *= i
    res //= j
    i -= 1
    j += 1
    return res


    a, b = map(int, input().split())
    print(C(a, b))

    处理更大的数据,用自带的函数快很多,前提是不用取模,用取模的话,还是上面这么写

    1
    2
    3
    4
    5
    6
    7
    8
    9
    from math import factorial as fact

    def C(a, b):
    A = fact(a)
    B = fact(a - b) * fact(b)
    return A // B

    a, b = map(int, input().split())
    print(C(a, b))
使用搜索:谷歌必应百度