Description
Background
Consider a specific set of comparable objects. Between two objects
a and
b, there exits one of the following three classified relations:
a =
b
a < b
b < a
a < b
b < a
Because relation '=' is symmetric, it is not repeated above.
So, with 3 objects (
a,
b,
c), there can exist 13 classified relations:
a = b = c a = b < c c < a = b a < b = c
b = c < a a = c < b b < a = c a < b < c
a < c < b b < a < c b < c < a c < a < b
c < b < a
b = c < a a = c < b b < a = c a < b < c
a < c < b b < a < c b < c < a c < a < b
c < b < a
Problem
Given
N, determine the number of different classified relations between
N objects.
Input
Includes many integers
N (in the range from 2 to 10), each number on one line. Ends with −1.
Output
For each
N of input, print the number of classified relations found, each number on one line.
Sample Input
input | output |
---|---|
2 3 -1 | 3 13 |
解题思路:dp[i][j]表示i个元素组成j个内部全等的集合,再插入一个元素时,dp[i+1][j]+=dp[i][j]*j就是这个元素插入到某个集合中,
<strong>dp[i+1][j+1]+=dp[i][j]*(j+1)插入到集合之间</strong>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[15][15],ans[11];
void DP()
{
memset(dp,0,sizeof(dp));
memset(ans,0,sizeof(ans));
dp[1][1]=1;
for(int i=1;i<=10;i++)
for(int j=1;j<=i;j++)
{
dp[i+1][j]+=dp[i][j]*j;
dp[i+1][j+1]+=dp[i][j]*(j+1);
}
for(int i=1;i<=10;i++)
for(int j=1;j<=i;j++)
ans[i]+=dp[i][j];
}
int main()
{
int n;
DP();
while(scanf("%d",&n),n!=-1)
{
printf("%d\n",ans[n]);
}
return 0;
}