uva 12034 Race(dp)

来源:互联网 发布:网络虚拟展厅 编辑:程序博客网 时间:2024/06/05 04:01

  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 ($ \le$1000), denoting the number of test cases. Each case starts with a line containing an integer n ( 1$ \le$n$ \le$1000).

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 

3123

Sample Output 

Case 1: 1Case 2: 3Case 3: 13

容易找到dp[i][j]表示i个数种选j个相同的方案数,转移方程详见代码。

#include <cstdio>#include <algorithm>#include <vector>#include <map>#include <queue>#include <iostream>#include <stack>#include <set>#include <cstring>#include <stdlib.h>#include <cmath>using namespace std;typedef long long LL;typedef pair<int, int> P;const int maxn = 1000 + 5;const int mod = 10056;int dp[maxn][maxn];void solve(){    memset(dp, 0, sizeof(dp));    dp[1][0] = dp[1][1] = 1;    for(int i = 2;i < maxn;i++){        dp[i][0] = (dp[i-1][0]*i) % mod;        dp[i][i] += dp[i][0];        for(int j = 1;j < i-1;j++){            dp[i][j] = ((i-j)*(dp[i-1][j] + dp[i-1][j-1])) % mod;            dp[i][i] += dp[i][j];        }        dp[i][i-1] = 1;        dp[i][i] = (dp[i][i] + dp[i][i-1]) % mod;    }}int main(){    int t,kase = 0;    solve();    scanf("%d", &t);    while(t--){        kase++;        int n;        scanf("%d", &n);        printf("Case %d: %d\n", kase, dp[n][n]);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 入厕纸把私处伤了一下怎么办? 夏天做月子用姨妈巾热怎么办 涂了痔疮膏后怕粘到内裤怎么办 眼罩里面的蓝色液体干了怎么办 新买的饮水机有塑料味怎么办 白色衣服被洗衣液染荧光了怎么办 衣服碰到了酒店的毛巾被单怎么办 防晒喷雾弄衣服上有荧光怎么办 剑网3重置版删除后有残留怎么办 在超市买到变质的水果怎么办 微信官方电话一直打不通怎么办 对方欠货款股东换了不还怎么办 闲鱼买的东西确认收货有问题怎么办 不让微信好友看到吃鸡的名字怎么办 金鹰贵宾积分卡过期了怎么办 小宝机器人一直停在联网界面怎么办 手机版的有道云笔记忘记邮箱怎么办 钡灌肠复查钡剂排空不良怎么办 两个月宝宝灌肠后不排便怎么办 一岁宝宝肠套叠灌肠后拉肚子怎么办 苹果手机自带的天气没有了怎么办? 衣服在洗衣机里忘记拿出来怎么办 苹果se手机系统占内存太大怎么办 客人把饭店老板打了民警怎么办 商场嫌品牌低端不让入驻怎么办 带着孩子坐飞机座位不在一起怎么办 公司老板跑路了员工该怎么办 超市买的衣服防盗扣忘记取了怎么办 在超市买的衣服那个扣没取怎么办啊 超市散称商品条码老记不住怎么办 app账号密码忘记了怎么办注销难 幼儿老师遇到家长比较孩子该怎么办 发的微信公众号内容重复了怎么办 招嫖诈骗微信转账被骗怎么办 朋友在深圳龙岗被传销骗了要怎么办 怀疑家里人被传销组织骗去了怎么办 怀疑家人被骗进传销了该怎么办 b本扣3分了怎么办2018 抖音里面就剩人头的视频怎么办 自学参加普通高考那小高考怎么办 如果小学生长胸只长了一个该怎么办