【HDOJ】1133 Buy the Ticket

大数,用DP也可以做,不过公式也比较好推倒。f(m,n) = m!*n!(C_m+n^n - C_m+n^(n-1))。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define MAXNUM 505
 5 
 6 int res[MAXNUM];
 7 
 8 int main() {
 9     int m, n, case_n = 0;
10     int i, j, carry, k, tmp;
11 
12     while (scanf("%d%d", &m, &n)!=EOF && (m||n)) {
13         ++case_n;
14         printf("Test #%d:\n", case_n);
15         if (n>m) {
16             printf("%d\n", 0);
17             continue;
18         }
19         memset(res, 0, sizeof(res));
20         // (m+n)!
21         res[0] = 1;
22         carry = 0;
23         for (i=1; i<=m+n; ++i) {
24             for (j=0; j<MAXNUM; ++j) {
25                 res[j] = i*res[j] + carry;
26                 carry = res[j] / 10;
27                 res[j] %= 10;
28             }
29         }
30         // *(m+1-n)
31         i = m+1-n;
32         carry = 0;
33         for (j=0; j<MAXNUM; ++j) {
34             res[j] = i * res[j] + carry;
35             carry = res[j] / 10;
36             res[j] %= 10;
37         }
38         // / (m+1)
39         k = MAXNUM-1;
40         while ( !res[k] )
41             --k;
42         j = tmp = 0;
43         for (i=k; i>=0; --i) {
44             tmp = 10*tmp + res[i];
45             if ( !j ) {
46                 if (tmp >= (m+1)) {
47                     printf("%d", tmp/(m+1));
48                     j = 1;
49                 }
50             } else
51                 printf("%d", tmp/(m+1));
52             tmp %= (m+1);
53         }
54         printf("\n");
55     }
56 
57     return 0;
58 }

 

 

 

posted on 2014-04-13 17:47  Bombe  阅读(126)  评论(0编辑  收藏  举报

导航