描述
There are N buttons on the console. Each button needs to be pushed exactly once. Each time you may push several buttons simultaneously.
Assume there are 4 buttons. You can first push button 1 and button 3 at one time, then push button 2 and button 4 at one time. It can be represented as a string “13-24”. Other pushing way may be “1-2-4-3”, “23-14” or “1234”. Note that “23-41” is the same as “23-14”.
Given the number N your task is to find the number of different valid pushing ways.
输入
An integer N. (1 <= N <= 1000)
输出
Output the number of different pushing ways. The answer would be very large so you only need to output the answer modulo 1000000007.
样例输入
3
样例输出
13
思路:
统计方案数的题目可以用dp来做,不妨我们设dp[i][j]表示 数字 1~i 分成 j 组一共有多少种方案。那么考虑 dp[i+1][j+1]等于多少? 在之前由于递推求得了 dp[i][j] 了,那么
- 一种情况是1~i 的数分成 j 组,然后第 i+1个数 独立成为一组 ,相当于在分好的 j 组中 插空就行了,j 个组 一共 j+1 个空,所以这种情况的方案数是dp[i][j]∗(j+1)dp[i][j]*(j+1)dp[i][j]∗(j+1)
- 另外一种情况是 1~i的数已经被分成了 j+1 个组,那么第 i+1 这个数,随便放到这 j+1中的任意一个组就行了,那么这种情况的方案数是 d[i][j+1]∗(j+1)d[i][j+1]*(j+1)d[i][j+1]∗(j+1)
综上就可以得出递推式:dp[i][j]=dp[i−1][j−1]∗j+dp[i−1][j]∗jdp[i][j] = dp[i-1][j-1]*j + dp[i-1][j]*jdp[i][j]=dp[i−1][j−1]∗j+dp[i−1][j]∗j
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1e3+5;
const int mod = 1e9+7;
typedef long long ll;
ll dp[maxn][maxn];
void solve(int n)
{
memset(dp,0,sizeof(dp));
for (int i = 1; i <= n; ++i)
dp[i][1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dp[i][j] = (dp[i-1][j-1]*j)%mod + (dp[i-1][j]*j)%mod;
dp[i][j] %= mod;
}
}
ll res = 0;
for (int i = 1; i <= n; ++i) {
res = (res + dp[n][i])%mod;
}
cout << res%mod << endl;
}
int main()
{
int n;
cin >> n;
solve(n);
return 0;
}