Codeforces 888D - Almost Identity Permutations 【数学模拟】

本文介绍了一种算法,用于计算特定条件下几乎恒等排列的数量。对于给定的整数n和k,算法能找出有多少种1到n的排列使得至少有n-k个元素位于其原本的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


D. Almost Identity Permutations


time limit per test 2 seconds

memory limit per test     256 megabytes


A permutation p of size n is an arraysuch that every integer from 1 to n occurs exactlyonce in this array.

Let's call a permutation analmost identitypermutation iff there existat least n - k indices i (1 ≤ i ≤ n) such that pi = i.

Your task is to count the number ofalmost identity permutationsfor given numbers n and k.

Input

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

Output

Print the number ofalmost identity permutationsfor 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和k计算满足至少有(n-k)个位置的值a[i]==i的1~n的全排列的个数。


【思路】


观察题目数据范围,发现k的取值范围只有1~4,故考虑能否直接分类讨论找出数学公式。


当k=1时,显然无法做到只有一个位置a[i]!=i,即只有一种全排列。


当k=2时,我们先去考虑有且只有两个位置a[i]!=i的数目,显然我们需要从1~n中取出一个二元组<i,j>(其中i<j),然后将他们每个二元组交换顺序即可,容易发现二元组数目为C(n,2),答案加上k=1的情况即可。


当k=3时,我们先去考虑有且只有三个位置a[i]!=i的数目,显然我们需要从1~n中取出一个三元组<i,j,k>(其中i<j<k),然后将他们每个三元组交换顺序且每个数都不在原来的位置上,容易发现三元组的数目为C(n,3),且对于每个二元组满足条件的排列数为2。


比如对于<1,2,3>来说,满足的交换顺序有<2,3,1>,<3,1,2>。


所以答案为三元组数目*2+(k=2的数目)+(k=1的数目)


当k=4时,同上面的分析可知,四元组的数目为C(n,4)


然后对于每个四元组,满足的排列数为9.


所以答案为四元组数目*9+(k=3的数目)+(k=2的数目)+(k=1的数目)


#include <cstdio>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 1000005;
const ll mod = 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n,k;

ll C(int n,int k)
{
    ll ans=1;
    for(int i=n;i>=n-k+1;i--)
    {
        ans*=i;
    }
    for(int i=1;i<=k;i++)
    {
        ans/=i;
    }
    return ans;
}

int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        if(k==1) puts("1");
        else if(k==2) printf("%I64d\n",C(n,2)+1);
        else if(k==3) printf("%I64d\n",C(n,2)+1+C(n,3)*2);
        else if(k==4) printf("%I64d\n",C(n,2)+1+C(n,3)*2+C(n,4)*9);
    }
    return 0;
}




### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值