您的位置:首页 > 其它

UVa 12034 Race

2016-09-22 00:13 441 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: