题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1261
解题思路很好想,设一共有n=n1+n2+...nk个字符,分别出现n1,n2,...nk次,则组合数有n!/(n1!n2!...nk!)种
难点在大数运算上
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 #include <cmath> 7 #include <queue> 8 #include <map> 9 #include <set> 10 #include <stack> 11 #include <list> 12 #include <vector> 13 14 using namespace std; 15 16 int ans[500]; 17 int has[27]; 18 int n, s; 19 20 void division(int n, int m) { 21 int c = 0; 22 for(int i = 0; i < 500; i++) { 23 c = ans[i] * n + c; 24 ans[i] = c % 10; 25 c /= 10; 26 } 27 c = 0; 28 for(int i = 499; i >= 0; i--) { 29 c = ans[i] + c * 10; 30 ans[i] = c / m; 31 c %= m; 32 } 33 } 34 35 int main() { 36 // freopen("in", "r", stdin); 37 while(~scanf("%d", &n) && n) { 38 s = 0; 39 memset(ans, 0, sizeof(ans)); 40 ans[0] = 1; 41 for(int i = 0; i < n; i++) { 42 scanf("%d", &has[i]); 43 for(int j = 1; j <= has[i]; j++) { 44 division(s+j, j); 45 } 46 s += has[i]; 47 } 48 int i; 49 for(i = 499; i >= 0; i--) { 50 if(ans[i]) { 51 break; 52 } 53 } 54 for(int j = i; j >= 0; j--) { 55 printf("%d", ans[j]); 56 } 57 printf("\n"); 58 } 59 }
时间: 2024-10-12 19:25:57