UVA 12034-Race
来源:互联网 发布:屠龙大陆翅膀进阶数据 编辑:程序博客网 时间:2024/06/05 05:49
Description
Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded and wandered around, even in their holidays. They passed several months in this way. But everything has an end. A holy person, Munsiji came into their life. Munsiji took them to derby (horse racing). Munsiji enjoyed the race, but as usual Disky and Sooma did their as usual task instead of passing some romantic moments. They were thinking- in how many ways a race can finish! Who knows, maybe this is their romance!
In a race there are n horses. You have to output the number of ways the race can finish. Note that, more than one horse may get the same position. For example, 2 horses can finish in 3 ways.
- Both first
- horse1 first and horse2 second
- horse2 first and horse1 second
Input
Input starts with an integer T ( 1000), denoting the number of test cases. Each case starts with a line containing an integer n ( 1n1000).Output
For each case, print the case number and the number of ways the race can finish. The result can be very large, print the result modulo 10056.Sample Input
3123
Sample Output
Case 1: 1Case 2: 3Case 3: 13
Source
Root :: Prominent Problemsetters :: Md. Mahbubul Hasan
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Counting ::Exercises: Intermediate
小结:
大概想法是递推,至于为什么是递推,因为题目分类就是递推(看到这里不要打我==),对于递归的想法,我感觉,对于每种情况的分类讨论是解决这类问题的关键。
就拿这道题目来说,在现有的名次中插入新个体是这种题目的常见思路。
令dp[i][j]=i个人组成的所有的名次的种类数目,那么对应的每个新插入的个体,对应2种情况:(1)增加现有的名次总数 (2)加入现有的任意一个名次中。
然后就会有以下递推方程:dp[i][j]=i==j?dp[i-1][j-1]*j:(dp[i-1][j]+dp[i-1][j-1])*j;
以下是AC代码:
//111,112,121,211,122,212,221,123,132,213,231,312,321//当n的值为3的时候,排名的种数为13#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;const int mod=10056;int dp[1005][1005];void solve(){ int ans,n; int t; scanf("%d",&t); for(int m=1;m<=t;m++) { scanf("%d",&n); ans=0; for(int i=1;i<=n;i++) { ans+=dp[n][i]; ans=ans%mod; } printf("Case %d: %d\n",m,ans); }}int main(){ int n; int mod=10056; memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int i=1;i<=1000;i++) { for(int j=1;j<=i;j++) { dp[i][j]=i==j?dp[i-1][j-1]*j:(dp[i-1][j]+dp[i-1][j-1])*j; dp[i][j]=dp[i][j]%mod; } } solve(); return 0;}
- UVa 12034 - Race
- UVA - 12034 Race
- UVA 12034 Race
- UVA 12034 Race
- UVA 12034-Race
- uva 12034 Race
- UVA 12034(p332)----Race
- uva 12034 Race
- UVA 12034 Race
- UVA 12034 Race
- UVa 12034 Race
- UVa 12034 Race
- UVA 12034 Race
- UVA 12034 Race
- uva 12034 Race(dp)
- UVA 12034 - Race(递推)
- uva 12034 - Race(dp计数)
- Race - UVa 12034 递推
- java集合框架---Map总结
- HDU1075 What Are You Talking About 【Map容器】
- java第三天,代码好多哦~~
- java数据引擎(八):应用一
- JAVA&Android随记
- UVA 12034-Race
- 例26 约瑟夫问题的变形(And Then There Was One,Japan 2007,LA 3882)
- Android中自定义对话框Dialog
- ArrayList和Vector的深度解读
- android开发步步为营之54:读取assets,raw文件夹下文件
- php安全
- 【HDU 1042】N! —— 高精度整数
- svn的建立以及使用
- thinkphp3.1.3多对多关联模型BUG修复