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

input output
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\n",ans);
}
return 0;
}

  

uralID: 196348LD

URAL 1142——Relations——————【dp】的更多相关文章

  1. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  4. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  5. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  6. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  8. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

  9. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

随机推荐

  1. Python的__getattr__方法学习

    内容部分来自网络 __getattr__函数的作用: 如果属性查找(attribute lookup)在实例以及对应的类中(通过__dict__)失败, 那么会调用到类的__getattr__函数: ...

  2. 编译原理-First集和Follow集

    刚学first集和follow集的时候,如果上课老师没有讲明白或者自己没听明白,自己看的时候还真是有点难理解,不过结合着具体的题目可以理解的更快. 先看一下两种集合的求法: First集合的求法:   ...

  3. HDU - 1754 I Hate It (线段树单点修改,求区间最大值)

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...

  4. dmp文件恢复oracle数据库

    –创建用户 create user anhui identified by anhui -给予用户权限 grant create session to anhuigrant connect,resou ...

  5. 解释器模式Interpreter详解

    原文链接:https://www.cnblogs.com/java-my-life/archive/2012/06/19/2552617.html 在阎宏博士的<JAVA与模式>一书中开头 ...

  6. P3240 [HNOI2015]实验比较 树形DP

    \(\color{#0066ff}{ 题目描述 }\) 小D 被邀请到实验室,做一个跟图片质量评价相关的主观实验.实验用到的图片集一共有 \(N\) 张图片,编号为 \(1\) 到\(N\).实验分若 ...

  7. P4013 数字梯形问题

    \(\color{#0066ff}{题目描述}\) 给定一个由 \(n\) 行数字组成的数字梯形如下图所示. 梯形的第一行有 \(m\) 个数字.从梯形的顶部的 \(m\) 个数字开始,在每个数字处可 ...

  8. Bicoloring UVA - 10004 二分图判断

    \(\color{#0066ff}{题目描述}\) 多组数据,n=0结束,每次一个n,m,之后是边,问你是不是二分图 \(\color{#0066ff}{输入样例}\) 3 3 0 1 1 2 2 0 ...

  9. luogu3911 最小公倍数之和(莫比乌斯反演)

    link 给定\(A_1,A_2,\dots,A_N\),求\(\sum_{i=1}^N\sum_{j=1}^Nlcm(A_i,A_j)\) \(1\le N\le 50000;1\le A_i\le ...

  10. 提交表单存在html标签报错-检测到有潜在危险的 Request.Form 值

    1..aspx页面 在.aspx文件头中加入这句<%@ Page validateRequest="false" %> 2.通用方法 修改web.config文件, & ...