zoukankan      html  css  js  c++  java
  • URAL 1142——Relations——————【dp】

    A - Relations
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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
    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

    inputoutput
    2
    3
    -1
    
    3
    13
    

    题目大意:给你n个人,让你给n个人排名,可以有并列,问你有多少种排名情况。

    解题思路:定义dp[i][j]表示j个人有i个名次,那么dp[1][i] = 1,而dp[i][i] = (i!) i的阶乘。dp[i][j] = i*dp[i][j-1] + i*dp[i-1][j-1]。表示当第j个人加入的话,要么第j个人跟某些人等名次,要么有一个单独的名次。当第j个人跟其他人等名次的时候,他可以选择i种名次中的任意一种,当第j个人有单独的名次的时候,他可以在i-1种名次形成的i个空中选一个。   思路转自:http://www.zhihu.com/question/30200444?sort=created

    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    typedef long long LL;
    LL fact[20],dp[20][20];
    void fac(){
        fact[0] = 1;
        for(LL i =1; i <= 15; i++){
            fact[i] = fact[i-1]*i;
        }
    }
    void prin(){
        for(int i = 1; i <= 12; i++){
            dp[1][i] = 1;
            dp[i][i] = fact[i];
        }
        for(int i = 2; i <= 12; i++){
            for(int j = i+1; j <= 12; j++){
                dp[i][j] = i*(dp[i][j-1] + dp[i-1][j-1]);
            }
        }
    }
    int main(){
        LL n;
        fac();
        prin();
        while(scanf("%lld",&n)!=EOF&&n!=-1){
            LL ans = 0;
            for(int i = 1; i <= n; i++){
                ans += dp[i][n];
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    

      

    uralID: 196348LD

  • 相关阅读:
    理解SVG坐标系统和变换: transform属性
    在svg文间画图过程中放大缩小图片后,坐标偏移问题
    理解SVG的缩放 偏移的计算公式
    svg 实践之屏幕坐标与svg元素坐标转换
    Winform 程序打包及安装
    使用bootstrap table小记(表格组件)
    MVC实现多级联动
    微信公众号开发之网页中及时获取当前用户Openid及注意事项
    微信公众号开发之网页授权获取用户基本信息
    微信公众号开发之自动消息回复和自定义菜单
  • 原文地址:https://www.cnblogs.com/chengsheng/p/5034047.html
Copyright © 2011-2022 走看看