题意:A,B两个人比赛,名次有三种情况(并列第一,AB,BA)。输入n,求n个人比赛时最后名次的可能数。

析:本来以为是数学题,排列组合,后来怎么想也不对。原来这是一个递推。。。

设n个人时答案为f(n)假设第一名有i(0< i <= n)个人,也就是有C(n, i)种,还剩下f(n-i)种可能,然后就so easy了。

f(n) = ΣC(n, i)f(n-i)。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #define mod %10056
  5.  
  6. using namespace std;
  7. const int maxn = 1001;
  8. int c[maxn+5][maxn+5], f[maxn+5];
  9.  
  10. void init(){
  11. for(int i = 0; i < maxn; i++)
  12. c[i][i] = 1, c[i][0] = 1;
  13. for(int i = 1; i < 1001; i++)
  14. for(int j = 1; j <= i; j++)
  15. c[i][j] = (c[i-1][j-1] + c[i-1][j]) mod;
  16.  
  17. }
  18.  
  19. void solve(){
  20. f[0] = 1, f[1] = 1, f[2] = 3;
  21. for(int i = 3; i < 1001; i++)
  22. for(int j = 1; j <= i; j++){
  23. int k = (c[i][j]*f[i-j]) mod;
  24. f[i] = (k+f[i]) mod;
  25. }
  26. }
  27.  
  28. int main(){
  29. memset(c, 0, sizeof(c));
  30. memset(f, 0, sizeof(f));
  31. init();
  32. solve();
  33. int n, T, cases = 0; cin >> T;
  34. while(T--){
  35. scanf("%d", &n);
  36. printf("Case %d: %d\n", ++cases, f[n]);
  37. }
  38. return 0;
  39. }

UVa 12034 Race (递推+组合数学)的更多相关文章

  1. UVa 12034 Race 递推?

    一开始是想排列组合做的,排列组合感觉确实可以推出公式,但是复杂度嘛.. dp[i][j]表示有i只马,j个名次的方法数,显然j<=i,然后递推公式就很好写了,一只马新加进来要么与任意一个名次的马 ...

  2. UVa 12034 - Race(递推 + 杨辉三角)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA 12034 Race (递推神马的)

    Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded ...

  4. UVA 12034 Race(递推)

    递推,f[i = i个名次][j = 共有j个人] = 方案数. 对于新加入的第j个人,如果并列之前的某个名次,那么i不变,有i个可供并列的名次选择,这部分是f[i][j-1]*i, 如果增加了一个名 ...

  5. UVa 10943 (数学 递推) How do you add?

    将K个不超过N的非负整数加起来,使它们的和为N,一共有多少种方法. 设d(i, j)表示j个不超过i的非负整数之和为i的方法数. d(i, j) = sum{ d(k, j-1) | 0 ≤ k ≤ ...

  6. UVa 10520【递推 搜索】

    UVa 10520 哇!简直恶心的递推,生推了半天..感觉题不难,但是恶心,不推出来又难受..一不小心还A了[]~( ̄▽ ̄)~*,AC的猝不及防... 先递推求出f[i][1](1<=i< ...

  7. Uva 10446【递推,dp】

    UVa 10446 求(n,bcak)递归次数.自己推出来了一个式子: 其实就是这个式子,但是不知道该怎么写,怕递归写法超时.其实直接递推就好,边界条件易得C(0,back)=1.C(1,back)= ...

  8. UVa 557 (概率 递推) Burger

    题意: 有两种汉堡给2n个孩子吃,每个孩子在吃之前要抛硬币决定吃哪一种汉堡.如果只剩一种汉堡,就不用抛硬币了. 求最后两个孩子吃到同一种汉堡的概率. 分析: 可以从反面思考,求最后两个孩子吃到不同汉堡 ...

  9. BZOJ4451 [Cerc2015]Frightful Formula 多项式 FFT 递推 组合数学

    原文链接http://www.cnblogs.com/zhouzhendong/p/8820963.html 题目传送门 - BZOJ4451 题意 给你一个$n\times n$矩阵的第一行和第一列 ...

随机推荐

  1. python: no module named bz2

    https://stackoverflow.com/questions/8115280/importerror-no-module-named-bz2-for-python-2-7-2 centos6 ...

  2. Nginx_status显示结果详解

    打开:http://aabb.com/nginx_status会有如下显示Active   connections: 2872server   accepts  handled requests294 ...

  3. html5中的SessionStorage 和localStorage

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...

  4. 分享至微信、QQ、微博、复制链接

    var share = { "tit": '您的朋友分享了文章', "desc": '分享来自百度文库,包含...', "pic": 'ht ...

  5. 【c++】c++中重载输出操作符,为什么要返回引用

    针对:ostream & operator <<(ostream & os, const ClassType &object) 说明几点: 1.第一个形参为对ost ...

  6. 35-面试:如何找出字符串的字典序全排列的第N种

    http://www.cnblogs.com/byrhuangqiang/p/3994499.html

  7. MVC仓储类Repository

    接口: using Common; using System; using System.Collections; using System.Collections.Generic; using Sy ...

  8. MVC中构建Linq条件、排序、Selector字段过滤

    代码: System.Linq.Expressions.Expression<Func<Domain.S_ROLE, bool>> expressWhere1 = (c =&g ...

  9. CookiesHelper

    /// <summary> ///CookiesHelper 的摘要说明 /// </summary> public class CookiesHelper { public ...

  10. How to Check if Linux (Ubuntu, Fedora Redhat, CentOS) is 32-bit or 64-bit

    The number of CPU instruction sets has kept growing, and likewise for the operating systems which ar ...