您的位置:首页 > 其它

[HDOJ1261]字串数

2015-09-02 23:46 495 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1261

解题思路很好想,设一共有n=n1+n2+...nk个字符,分别出现n1,n2,...nk次,则组合数有n!/(n1!n2!...nk!)种

难点在大数运算上

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <vector>

using namespace std;

int ans[500];
int has[27];
int n, s;

void division(int n, int m) {
int c = 0;
for(int i = 0; i < 500; i++) {
c = ans[i] * n + c;
ans[i] = c % 10;
c /= 10;
}
c = 0;
for(int i = 499; i >= 0; i--) {
c = ans[i] + c * 10;
ans[i] = c / m;
c %= m;
}
}

int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n) && n) {
s = 0;
memset(ans, 0, sizeof(ans));
ans[0] = 1;
for(int i = 0; i < n; i++) {
scanf("%d", &has[i]);
for(int j = 1; j <= has[i]; j++) {
division(s+j, j);
}
s += has[i];
}
int i;
for(i = 499; i >= 0; i--) {
if(ans[i]) {
break;
}
}
for(int j = i; j >= 0; j--) {
printf("%d", ans[j]);
}
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: