码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1142. Relations(dp啊)

时间:2015-02-08 21:57:15      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:dp   数学   ural   

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1142


1142. Relations

Time limit: 1.0 second
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
Because relation ‘=‘ is symmetric, it is not repeated above.
So, with 3 objects (abc), 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

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 个数字,
等号之间可以看成是一个整体,

求方案数?

PS:

dp[i][j]:表示将i个数分成j个有序集合的方案数
1、如果将第i个数新建一个集合插进某个位置,
方案数为:dp[i-1][j-1]*j
2、如果将第i个数插进原有的集合,
方案数为:dp[i-1][j]*j

所以:dp[i][j]=dp[i-1][j-1]*j + dp[i-1][j]*j

代码如下:

#include <cstdio>
#include <cstring>
int dp[147][147];
int ans[47];
//dp[i][j] = dp[i-1][j]*j + dp[i-1][j-1]*j;
void init()
{
    memset(ans, 0,sizeof(ans));
    dp[1][0] = 0;
    dp[1][1] = 1;
    for(int i = 2; i <= 10; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            dp[i][j] = dp[i-1][j]*j + dp[i-1][j-1]*j;
            ans[i] += dp[i][j];
        }
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n == -1)
            break;
        init();
        printf("%d\n",ans[n]);
    }
    return 0;
}
/*
2
3
4
5
6
7
8
9
10
*/


URAL 1142. Relations(dp啊)

标签:dp   数学   ural   

原文地址:http://blog.csdn.net/u012860063/article/details/43645983

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!