UVA 12034 Race (递推神马的)

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.

  1. Both first
  2. horse1 first and horse2 second
  3. 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

3
1
2
3

Sample Output

Case 1: 1
Case 2: 3
Case 3: 13
对,又是递推题目:我们dp[i][j]表示i个马,分j次到达的方法;
考虑状态转移:dp[i][j]=j*(dp[i-1][j](第i个马和前面的马搭伙到达)+dp[i-1][j-1](第i个马单独算一次))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int MOD=10056;
int dp[1100][1100];
int ans[1100];
void init()
{
    memset(dp,0,sizeof(dp));
    memset(ans,0,sizeof(ans));
    dp[0][0]=1;
    for(int i=1;i<=1000;i++)
    {
        int sum=0;
        for(int j=1;j<=i;j++)
        {
            dp[i][j]+=(dp[i-1][j]+dp[i-1][j-1])%MOD*j%MOD;//小心心爆int
            sum=(sum%MOD+dp[i][j])%MOD;
        }
        ans[i]=sum;
    }
}
int main()
{
    init();
    int n,t;
    int cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("Case %d: %d\n",cas++,ans[n]);
    }
    return 0;
}

时间: 2024-08-02 11:04:32

UVA 12034 Race (递推神马的)的相关文章

UVA 12034 - Race(递推)

UVA 12034 - Race 题目链接 题意:给定n匹马,要求出可能的排名情况(可能并列) 思路:递推,dp[i][j]表示i匹马的时候有j种不同名次,那么dp[i][j]可以由dp[i - 1][j - 1]插入j个不同位置得来,或者由dp[i - 1][j]放入已有j的名次得来,得到递推式dp[i][j] = j * (dp[i - 1][j - 1] + dp[i - 1][j]); 然后对于n的答案为sum{dp[n][j]} (1 <= j <= n) 代码: #include

UVA 12034(递推&amp;递归_I题)解题报告

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

UVA 10237 - Bishops(递推)

UVA 10237 - Bishops 题目链接 题意:问一个n * n棋盘能放k个主教(攻击斜线)的方案数. 思路:递推,首先考虑一个问题,在一个n?n棋盘上,放k个车的方案数. 那么设dp[i][j]为i行用了j个车的方案数,由于每行只能放一个车,那么考虑i行放不放车,如果放车,那么能放的位置有n?(j?1)个位置,为dp[i?1][j?1]?(n?(j?1)). 如果不放那么情况为dp[i?1][j]. 所以递推式为dp[i][j]=dp[i][j?1]+dp[i?1][j?1]?(n?(

UVA 1425 - Metal(递推)

UVA 1425 - Metal 题目链接 题意:给定一个金属板,上面有一些点,现在有一台切割机,要切割出单调四边形,由所有点组成,问有多少种情况. 思路:递推,设dp[i][j],i为上面点,j为下面点,现在多添加一个点k进来,那么原来的dp[i][j]必然要有一维为k - 1,枚举另外一维就是所有情况.然后再添加点进来的过程中还要考虑能不能加进来,写一个判断函数,把连接线之间所有点枚举一边利用向量叉积去判断即可,如果是上面的线,就不能有点在上面,如果是下面的线,就不能有点再下面. 代码: #

uva 11375 - Matches(递推)

题目链接:11375 - Matches 题目大意:给出n根火柴,问说能组成多少种数字,要求说0不能打头. 解题思路:d[i]表示i根火柴能够组成的数量,d[i+c[j]] = d[i+c[j]] + d[i]; 最后dp[i]表示小于等于i根火柴能组成的数量,dp[i]=∑jidp[j]. 高精度. #include <cstdio> #include <cstring> #include <iostream> using namespace std; const i

uva 279 - Spin(递推)

题目链接:uva 279 - Spin 题目大意:进行一个游戏,给出初始状态,要求问说最少多少步可以让所有的环移动出来.移动规则如图所示. 解题思路:一开始以为是隐式图搜索,写完TLE了.后来发现这道题和汉诺塔是一个思路,都是采取最优策略,并且说左边环的状态不会影响右边环.所以dp[i]表示从右边数,第i个为v,其他均为h的步数(由全h变换至). 模拟最优过程有dp[i]=dp[i?1]?2+i?2?1 对已给定状态,可看做由全h变换到该状态的步数.根据容斥原理,第奇数个v为加,偶数个v为减.最

UVA - 624CD(递推+ 路径打印)

题目: UVA - 624CD(递推+ 路径打印) 题目大意:给出一组数据,给定一个N,问这些数据能否拼凑出不大于N的最接近N的数据,可以的话输出最接近N的数据,并且打印出最长路径(要求要找输入的顺序). 解题思路:dp[j]:代表凑出J这个数值最多需要几个数.d[j] = Max (d[j - v[i]] + 1. 打印路径,如果取得是最小值,那么顺着dp标记的值的减小就可以找到路径,但是取的是最大值,这样它的下一个并不能直接靠dp数组的值来判断,而是要判断到最后是否最终的值等于0.用回溯.

UVa 12034 Race (递推+组合数学)

题意: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). 代码如下: #include <iostream> #include <cstdio> #include

UVa 1645 Count (递推,数论)

题意:给定一棵 n 个结点的有根树,使得每个深度中所有结点的子结点数相同.求多棵这样的树. 析:首先这棵树是有根的,那么肯定有一个根结点,然后剩下的再看能不能再分成深度相同的子树,也就是说是不是它的约数.那么答案就有了, 我们只要去计算n-1的约数有多少棵不同的树,然后就有递推式了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <str