D. Almost Identity Permutations(dp)

本文探讨了如何计算特定条件下几乎恒等排列的数量,即在一个由1到n组成的序列中,找出有多少种排列方式使恰好有n-k个元素位于其原本的位置上。通过动态规划的方法实现了这一计算,并给出了具体的实现代码。

D. Almost Identity Permutations
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.

Let’s call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that pi = i.

Your task is to count the number of almost identity permutations for given numbers n and k.

Input
The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4).

Output
Print the number of almost identity permutations for given n and k.

Examples
input
4 1
output
1
input
4 2
output
7
input
5 3
output
31
input
5 4
output
76

题意:一个长度为n,由1~n组成的p序列(pi != pj ,i != j)。问有多少组排列方式使得只有k个pi = i。

解题思路:如代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;

const int maxn = 1005;
LL dp[maxn][maxn];//dp[i][j]:长度为i,由1 ~ i组成的序列p(序列中数都不相同),满足只有j个pm = m的排列方式数.

void Init(){
    dp[4][4] = 1;
    dp[4][3] = 0;
    dp[4][2] = 6;
    dp[4][1] = 8;
    dp[4][0] = 9;
}

int main(){
    Init();
    LL n,k,sum = 0;
    scanf("%I64d %I64d",&n,&k);
    for(LL i = 5;i <= n;i++){
        for(LL j = 0;j <= i;j++){
            if(j == 0) dp[i][j] = (i - 1) * dp[i - 1][j] + dp[i - 1][1];
            else{
                dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] * (i - 1 - j) + dp[i - 1][j + 1] * (j + 1);
            }
        }
    }
    for(int i = n - k;i <= n;i++){
        sum += dp[n][i];
    }
    printf("%I64d\n",sum);
}
### Python 中 `itertools.permutations` 的用法 `itertools.permutations` 是 Python 标准库中的一个函数,用于生成给定可迭代对象的所有可能排列。这些排列按照字典序排序输出,因此如果输入的可迭代对象本身已排序,则生成的结果也会保持有序。 以下是关于该函数的具体说明: #### 函数签名 ```python itertools.permutations(iterable, r=None) ``` - 参数 `iterable` 表示要进行排列操作的目标序列。 - 参数 `r` 表示每次排列所选取的元素数量,默认为 None,即取整个序列长度作为排列单位[^1]。 #### 返回值 返回的是一个生成器对象,可以通过将其转换成列表来查看所有排列结果。 --- #### 示例代码展示 以下是一些具体的例子,帮助理解其功能: ```python from itertools import permutations # 示例 1: 对 ['1', '2', '3'] 进行全排列 result = list(permutations(['1', '2', '3'])) print(result) # 输出: # [('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), # ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')] # 示例 2: 只选择两个元素进行排列 result_with_r = list(permutations(['1', '2', '3'], 2)) print(result_with_r) # 输出: # [('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), # ('3', '1'), ('3', '2')] # 示例 3: 字符串类型的排列 string_permutation = list(permutations('abc', 3)) print(string_permutation) # 输出: # [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), # ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')] ``` 上述代码展示了不同场景下的排列方式以及参数的作用[^2]。 --- #### 特性总结 1. **字典顺序**:当输入数据已经按升序排列时,生成的排列会遵循相同的字典顺序。 2. **性能高效**:由于返回的是生成器而非完整的列表,可以节省内存开销,在处理大规模数据集时尤为有用。 3. **灵活性强**:通过调整参数 `r`,能够控制每次排列涉及的元素数目。 --- #### 注意事项 需要注意的一点是,对于重复元素较多的情况,`permutations` 不会对相同项去重。例如: ```python duplicate_result = list(permutations([1, 1, 2])) print(duplicate_result) # 输出: # [(1, 1, 2), (1, 2, 1), (1, 1, 2), # (1, 2, 1), (2, 1, 1), (2, 1, 1)] ``` 可以看到存在重复条目。若希望去除重复排列,需额外引入集合或其他方法实现过滤。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值