rosalind练习题二十七

# Problem

# A partial permutation is an ordering of only k objects taken from a collection containing n objects (i.e., k≤n). For example, one partial permutation of three of the first eight positive integers is given by (5,7,2).

# The statistic P(n,k) counts the total number of partial permutations of k objects that can be formed from a collection of n objects. Note that P(n,n) is just the number of permutations of n objects, which we found to be equal to n!=n(n−1)(n−2)⋯(3)(2) in “Enumerating Gene Orders”.

# Given: Positive integers n and k such that 100≥n>0 and 10≥k>0.

# Return: The total number of partial permutations P(n,k), modulo 1,000,000.

# Sample Dataset

# 21 7

# Sample Output

# 51200

# 这道题目中,给定两个正整数 n 和 k(满足 100≥n>0、10≥k>0),定义 P(n,k) 为从 n 个不同的对象中取 k 个对象进行排列的方案数,其中对象排列的顺序有限制(即只能考虑部分的排列方案),计算并输出 P(n,k) 对 1,000,000 取模后的结果。

# 具体来说,P(n,k) 的计算方式为:P(n,k) = n*(n-1)...(n-k+1)。需要注意的是,该计算结果需要对 1,000,000 取模。

def partial_permutations(n, k):

# 初始化P(n,k)为1

    P =1

    # 计算 P(n,k) 为n*(n-1)* ... *(n-k+1)

    for iin range(n, n - k, -1):

P = (P * i) %1000000

    return P

print(partial_permutations(21, 7))

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容