1142. Relations
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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 | output |
---|---|
2 3 -1 | 3 13 |
题意: 输入一个n。代表有n个数。输出他们所有的不同关系 有多少种。
做法:开一个dp[ i ][ j ] i表示当前状态有几个字母,j表示当前状态有多少个不同的数 (也就是小于号+1)。
如:a < b < c a < c < b b < a < c b < c < a c < a < b
上面这些是有 有两个等于的状态, 如果想再加一个不同于a,b,c的一个数d。 拿a < b < c 来说,可以有4种方法 d<a<b<c或者 a <d< b < c 或者 a<b<d<c 或者 a<b<c<d
而加和abc 某一个数相同的d 拿 拿a < b < c 来说 ,方法有3种 d=a<b<c或者 a <d=b < c 或者 a<b<d=c
再试试2推到3 或3推到4 的别的状态,
可以发现 从上一个状态 递归到 i状态,要乘上 i 状态 有多少不同数字,也就是乘上j 。
可以 有dp[ i ][ j ]= dp[ i - 1 ] [ j- 1 ]*j + dp[ i - 1 ] [ j ] *j
上面这表示加了一个不同的数 上面的表示加了一个和原来数中某数相等的数
#include<stdio.h>
#include<string.h>
int dp[11][11]={0};//i 几个字母 j 有几个不相等的字母(i-等于号数)
int main()
{
int n;
dp[1][1]=1;
int ans[11];
for(int i=2;i<=10;i++)
{
ans[i]=0;
for(int j=1;j<=10;j++)
{
dp[i][j]=dp[i-1][j-1]*j+dp[i-1][j]*j;
//加一个小于号 加一个等于号
ans[i]+=dp[i][j];
}
}
while(scanf("%d",&n),n!=-1)
{
printf("%d\n",ans[n]);
}
return 0;
}