24h購物| | PChome| 登入
2012-12-21 16:36:51| 人氣1,484| 回應0 | 上一篇 | 下一篇

[UVA][dp] 12034 - Race

推薦 0 收藏 0 轉貼0 訂閱站台


  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 

3
1
2
3

Sample Output 

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

dp[i][j] i 匹馬 j 個不同名次。
dp[i][j] = dp[i-1][j]*j + dp[i-1][j-1]*j;
// 前 i-1 匹馬 j 個名次並列其中 j 個名次,前 i-1 匹馬 j-1 個名次增加 j 種。

#include <stdio.h>
#define mod 10056
int dp[1005][1005], res[1005];
int main() {
    dp[0][0] = 1;
    int i, j;
    for(i = 1; i <= 1001; i++) {
        for(j = 1; j <= i; j++) {
            dp[i][j] = dp[i-1][j]*j + dp[i-1][j-1]*j;
            dp[i][j] %= mod;
            res[i] += dp[i][j];
            res[i] %= mod;
        }
    }
    scanf("%*d");
    i = 0;
    while(scanf("%d", &j) == 1)
        printf("Case %d: %d\n", ++i, res[j]);
    return 0;
}



台長: Morris
人氣(1,484) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][塊狀表] 12003 - Array Transformer
此分類上一篇:[UVA][因數個數] 12005 - Find Solutions

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文