【问题】
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;
}