UVA 12034 Race (遞推神馬的),12034神馬

來源:互聯網
上載者:User

UVA 12034 Race (遞推神馬的),12034神馬

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 ( 1 n1000).

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次到達的方法;
考慮狀態轉移: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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.