[UVA - 12034] Race 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

题目链接(vjudge):https://vjudge.net/problem/UVA-12034

题目大意:

A,B两人赛跑,可能出现三种情况:

1、A,B并列第一  2、A第一,B第二  3、B第一,A第二

现在有N个人赛跑,问可能出现多少种情况,答案对10056取模。

输入格式:

第一行一个数字T(1<=T<=1000),表示数据组数。

接下来T行,每行一个数字N(1<=N<=1000),表示赛跑的人数。

输出格式:

对于每个询问,输出当前的问题序号和答案。

Sample Input

3

1

2

3

Sample Output

Case 1: 1

Case 2: 3

Case 3: 13

分析:

递推。假设有n个人参加赛跑,其中x个人并列第一,那么情况数=C(n,x)*f(n-x)

枚举x计算出所有的情况,加和即可。

刘汝佳《算法竞赛入门经典》

AC代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5
 6 const int MOD = 10056;
 7
 8 inline void read(int &x)
 9 {
10     char ch = getchar(),c = ch;x = 0;
11     while(ch < ‘0‘ || ch > ‘9‘) c = ch,ch = getchar();
12     while(ch <= ‘9‘ && ch >= ‘0‘) x = (x<<1)+(x<<3)+ch-‘0‘,ch = getchar();
13     if(c == ‘-‘) x = -x;
14 }
15
16 int f[1005],c[1005][1005];
17
18 inline void init()
19 {
20     int n = 1000;
21
22     for(int i = 0;i <= n;++ i)
23     {//组合公式
24         c[i][0] = 1,c[i][i] = 1;
25         for(int j = 1;j < i;++ j)
26             c[i][j] = (c[i-1][j]+c[i-1][j-1])%MOD;
27     }
28     f[0] = 1;
29     for(int i = 1;i <= n;++ i)
30     {//i为参加比赛的总人数,j为第x名的人数
31         for(int j = 1;j <= i;++ j)
32             f[i] = (f[i]+(c[i][j]*f[i-j])%MOD)%MOD;
33     }
34 }
35
36 int main()
37 {
38     int t,n;
39     init();
40     read(t);
41     for(int T = 1;T <= t;++ T)
42     {
43         read(n);
44         printf("Case %d: %d\n",T,f[n]);
45     }
46     return 0;
47 }
时间: 2024-10-29 19:06:18

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

uva 12034 Race

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma

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 11762 - Race to 1(马尔可夫)

题目链接:uva 11762 - Race to 1 题目大意:给出一个整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/P,否则N不变.问平均情况下需要多少次选择,才能把N变成1. 解题思路:马尔可夫,例如N=6时,f(6)=1+f(6)?13+f(4)?13+f(2)?13,1是只第一次转移,后面分别对应的是选择5,2,3的情况.所以有f(x)=∑f(x/y)+p(x)g(x),p(x)为不超过x的素数个数,g(x)为是x因子的素数个数. #include <

UVA 11762 - Race to 1(概率)

UVA 11762 - Race to 1 题意:给定一个n,每次随即选择一个n以内的质数,如果不是质因子,就保持不变,如果是的话,就把n除掉该因子,问n变成1的次数的期望值 思路:tot为总的质数,cnt为质因子个数,那么f(n)=(1?cnt/tot)?f(n)+∑f(n/prime)?(1/tot),然后利用记忆化搜索去做即可 代码: #include <stdio.h> #include <string.h> const int N = 1000005; int t, n,

UVa 10152 - ShellSort 题解

按他的方法排序,每次移动一个数到顶点,排成需要的序列. Problem D: ShellSort He made each turtle stand on another one's back And he piled them all up in a nine-turtle stack. And then Yertle climbed up. He sat down on the pile. What a wonderful view! He could see 'most a mile! T

UVA - 11762 - Race to 1 记忆化概率

Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can bedivided by at least one prime number less than or equal to that number. So, he is now playing withthis property. He selects a number N. And he calls th

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

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