TJU 4087. box

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   tar   

題目:Tuhao and his two small partners participated in the tournament.But in the end, they lost the chance to finish a mathematical problem.After the competition,Tuhao recalled that year in high school math class on a variety of tragedy.

One day, the math teacher give Tuhao a question:if you have 5 boxes in a line(every boxes are different) and you have 4 kinds of balls.you must put an ball in every box, and all balls can take unlimited. How many kinds of way of placing? Tuhao solve this problem easily.But he has another question to you.if we have N boxes, and we have m kind of balls, and you can‘t use all kinds of balls because Tuhao must have one kind ball at least to play with his partners... 

Input

There will be multiple cases to consider. The first input will be a number T(0 < T ≤ 50) indicating how many cases with which you will deal. Following this number will be pairs of integers giving values for N and M, in that order. You are guaranteed that 1 ≤ N < 500, 1 ≤ M < 10, Each N M pair will occur on a line of its own. N and M will be separated by a single space.

Output

For each case display a line containing the case number (starting with 1 and increasing sequentially), and the kinds of way of placing. The desired format is illustrated in the sample shown below.(p.s. the answer should be module 200000007)

Sample Input
2 1 1 8 3 

Sample Output
Case 1: 0 Case 2: 765 
一道推斷題,每次拿到這樣的題都手賤的去寫方程運算式,結果越來越..

解析:定義F[I][J]表示前I個格子用了J種顏色,有如下方程:

F[I][J]=F[I-1][J-1]*(M-(J-1))+F[I-1][J]*J;

F[I-1][J-1]*(M-(J-1)):加上一種新的顏色的方案數,(M-(J-1))是剩下沒用的顏色數

F[I-1][J]*J:從以前選了J種顏色中選一種、

 開始F[I][1]=M;我為了避免處理麻煩,從I=2時開始操作;

代碼要精簡:^^;
這是未加工的部分
        for (int i=1;i<=n;i++)
                f[i][1]=1;
        for (int i=2;i<=n;i++)
            for (int j=1;j<=m;j++)
            f[i][j]=(f[i-1][j]*j+f[i-1][j-1]*(m-j+1))%maxn;

        int ans=0;
        for (int i=1;i<m;i++)
            ans=(ans+f[n][i])%maxn;
        printf("%d\n",ans*m%maxn);
    }
    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.