【HDOJ】1261 字串数

这道题目居然是大数。。。最开始审题,发现公式为(sum{A1...An})! / (A1!*A2*...AN!)。所以需要处理乘法大数,显然不会,看了一下别人的模板。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <string>
 6 #include <queue>
 7 #include <algorithm>
 8 #include <map>
 9 #include <iomanip>
10 using namespace std;
11 
12 
13 #define INF 99999999
14 #define MAX 27
15 
16 short s[MAX];
17 int sub[100];
18 
19 void mult(int *sub,int sum,int m){
20     int Base=10000, p=0;
21     for (int i=1; i<=sub[0]; ++i){
22         sub[i] = sub[i] * sum+p;
23         p = sub[i] / Base;
24         sub[i] = sub[i] % Base;
25     }
26     while (p){
27         sub[0]++;
28         sub[sub[0]] = p % Base;
29         p = p / Base;
30     }
31     for(int i=sub[0]; i>=1; --i){
32         sub[i] += p;
33         p = (sub[i]%m) * Base;
34         sub[i] = sub[i] / m;
35     }
36     while (!sub[sub[0]])
37         sub[0]--;
38 }
39 
40 void chg(int n,int m){
41     for(int i=1; i<=m; ++i)
42         mult(sub, n-m+i, i);//每次sub[]*(n-m+i)/i.
43     return;
44 }
45 
46 int main() {
47     int n;
48 
49     while (cin >>n && n){
50         int sum = 0;
51 
52         sub[0] = sub[1] = 1;
53         for (int i=0; i<n; ++i){
54             cin >>s[i];
55             sum += s[i];
56         }
57         for (int i=0; i<n-1; ++i){
58             chg(sum, s[i]);
59             sum -=s [i];
60         }
61         cout <<sub[sub[0]];
62         for (int i=sub[0]-1; i>=1; --i)
63             cout<<setfill('0')<<setw(4)<<sub[i];
64 
65         cout<<endl;
66     }
67 
68     return 0;
69 }

 

posted on 2014-03-23 16:41  Bombe  阅读(174)  评论(0编辑  收藏  举报

导航