UVa 12034 Race

本文解析了UVa12034赛马问题的算法实现,通过计算不同马匹可能获得相同名次的方式数量,介绍了如何使用动态规划与组合数学的方法求解。针对大输入规模,文章还提供了模运算下的高效解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 nish! 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 nish. Note that, more
than one horse may get the same position. For example, 2 horses can
nish in 3 ways.
1. Both rst
2. horse 1 rst and horse 2 second
3. horse 2 rst and horse 1 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 (1 n 1000). Output For each case,
print the case number and the number of ways the race can nish. The
result can be very large, print the result modulo 10056.


【分析】

考虑n个人中有i个人拿了第一名,方案数有C(n,i)*f[n-i]。
对i=1..n求和即可。
注意模数不是质数,不能求逆元,求组合数应该用递推


【代码】

//UVa 12034 Race
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1005;
const int mod=10056;
int n,m,T;
int c[mxn][mxn],dp[mxn];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
    int i,j;
    fo(i,0,1000) c[i][0]=1;
    fo(i,1,1000)
      fo(j,1,i)
        c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    dp[0]=dp[1]=1;
    fo(i,2,1000)
      fo(j,1,i)
        dp[i]=(dp[i]+c[i][j]*dp[i-j])%mod;
    T=read();
    fo(i,1,T)
    {
        j=read();
        printf("Case %d: %d\n",i,dp[j]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值