Permutation Counting

【问题】

Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the amount of elements where ai > i. For example, the E-value of permutation {1, 3, 2, 4} is 1, while the E-value of {4, 3, 2, 1} is 2. You are requested to find how many permutations of {1, 2, …, N} whose E-value is exactly k.

【输入】

There are several test cases, and one line for each case, which contains two integers, N and k. (1 <= N <= 1000, 0 <= k <= N).

【输出】

Output one line for each case. For the answer may be quite huge, you need to output the answer module 1,000,000,007.

【思路】

题意是n个数,有k个数a[k]>k,问有多少种情况
这个题可以考虑dp[i][j] 前i个数有j个数满足a[k]>k,然后表示为放第i个数
可以考虑到有三种情况:
1、第i个数放在第i位,这样种类数不会增加
2、第i个数与满足a[k]>k的数换位置,这样种类数还是不会改变
3、将第i个数与a[k]<=k的数互换,这样会多i-j种情况

具体看代码

【源代码】

#include<iostream>
using namespace std;
#define mod 1000000007
#define ll long long
ll dp[1100][1100];
int main()
{
    int n,k;
    for(int i=1; i<=1000; i++)//初始化
    {
        dp[i][0]=1;
        for(int j=1; j<i; j++)
            dp[i][j]=(dp[i-1][j]+dp[i-1][j]*j+dp[i-1][j-1]*(i-j))%mod;
    }
    while(cin >> n >> k)
        cout << dp[n][k] <<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅梦曾倾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值