Description

一个A和两个B一共可以组成三种字符串:"ABB","BAB","BBA".
给定若干字母和它们相应的个数,计算一共可以组成多少个不同的字符串.
 

Input

每组测试数据分两行,第一行为n(1<=n<=26),表示不同字母的个数,第二行为n个数A1,A2,...,An(1<=Ai<=12),表示每种字母的个数.测试数据以n=0为结束.
 

Output

对于每一组测试数据,输出一个m,表示一共有多少种字符串.
 

Sample Input

2
1 2
3
2 2 2
0
 

Sample Output

3
90

可以轻易推出公式 :(n1+n2+n3+...nn)!/(n1!*n2!*...*nn!);

因为15!还在long long的范围之内,可以先定义一个数组f[15]保存1~15的阶乘,接着就是将(n1+n2+n3+...nn)!计算出来并存到数组内,接着就是大数除法了(相当于一个大数除一个小数)。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <string>
#include <queue> using namespace std;
#define SIZE 30
typedef long long ll ;
int d[SIZE] ;
int ans[] , f[];
void multiply(int c){
ans[] = ans[] = ;
for(int i = ; i <= c ; ++i){
int r = ;
for(int j = ; j <= ans[] ; ++j){
ans[j] *= i ;
ans[j] += r ;
r = ans[j]/ ;
ans[j] %= ;
}
if(r != ){
while(r){
ans[ans[]+] += r% ;
ans[] = ans[]+ ;
r /= ;
}
}
}
} void divide(int n){
for(int i = ; i < n ; ++i){
if(d[i] == ) continue ;
ll r = ;
for(int j = ans[] ; j > ; --j){
r = r* + ans[j] ;
ans[j] = (int)(r/f[d[i]]) ;
r %= f[d[i]] ;
}
int j = ans[] ;
while(!ans[j--]) ;
ans[] = j+ ;
}
} int main(){
int n ;
f[] = f[] = ;
for(int i = ; i < ; ++i)
f[i] = f[i-]*i ;
while(scanf("%d",&n) && n){
int c = ;
memset(ans,,sizeof(ans)) ;
for(int i = ; i < n ; ++i){
scanf("%d",&d[i]) ;
c += d[i] ;
}
multiply(c) ;
divide(n) ;
for(int i = ans[] ; i > ; --i)
printf("%d",ans[i]) ;
puts("") ;
}
return ;
}

2017-3-4再做这道题,用了Java~~哈哈

import java.math.BigInteger;

/**
*
* @author Asimple
*
*/ import java.util.Scanner;
public class Main{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n;
while( sc.hasNext() ) {
n = sc.nextInt();
if( n == 0 ) break;
int sum = 0;
BigInteger a = BigInteger.valueOf(1);
for(int i=0; i<n; i++) {
int num = sc.nextInt();
sum += num;
a = a.multiply(dd(num));
}
BigInteger b = dd(sum);
b = b.divide(a);
System.out.println(b.toString());
}
} public static BigInteger dd(int x) {
BigInteger a = BigInteger.valueOf(1) ;
for(int i=2; i<=x; i++) {
a = a.multiply(BigInteger.valueOf(i));
}
return a;
}
}

ACM题目————字串数的更多相关文章

  1. 字串数_hdu_1261(大数极致).java

    字串数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  2. HDOJ 1261 字串数

    JAVA大数.... 字串数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. HDU 1261 字串数(排列组合)

    字串数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  4. 题解报告:hdu 1261 字串数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1261 Problem Description 一个A和两个B一共可以组成三种字符串:"ABB ...

  5. hdu 1261 字串数

    解题思路:ACM紫书 第十章 P319 有重复元素的全排列 答案: 所有数的和的阶乘 除以 每个数阶乘的乘积 因为给定 (26*12)! 会爆掉(long long),这里用java 的BigInte ...

  6. 【字符串】BZOJ上面几个AC自动机求最为字串出现次数的题目

    (一下只供自己复习用,目的是对比这几个题,所以写得不详细.需要细节的可以参考其他博主) [BZOJ3172:单词] 题目: 某人读论文,一篇论文是由许多(N)单词组成.但他发现一个单词会在论文中出现很 ...

  7. hdu 4333 扩展kmp+kmp重复字串去重

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 关于kmp next数组求最短重复字串问题请看:http://www.cnblogs.com/z ...

  8. Day07 - Ruby比一比:Symbol符号与String字串

    前情提要: 第六天我们透过Ruby代码练习public,protected和privatemethod时,发现冒号在前面的参数,:mydraft,:myspace,这些就是符号Symbol.在今天,我 ...

  9. POJ - 2774~POJ - 3415 后缀数组求解公共字串问题

    POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...

随机推荐

  1. Java基础之创建窗口——向窗口中添加菜单(Sketcher)

    控制台程序. JMenuBar对象表示放在窗口顶部的菜单栏.可以为JMenuBar对象添加JMenu或JMenuItem对象,它们都显示在菜单栏上.JMenu对象是带有标签的菜单,单击就可以显示一列菜 ...

  2. spawn-fcgi

    spawn-fcgi与PHP-FPM 前面介绍过,FastCGI接口方式在脚本解析服务器上启动一个或者多个守护进程对动态脚本进行解析,这些进程就是FastCGI进程管理器,或者称为FastCGI引擎. ...

  3. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  4. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  5. SQL order 排序

    select * from emp;

  6. 字典:NSDictionary的应用举例

    字典就是关键字及其定义(描述)的集合.Cocoa中的实现字典的集合NSDictionary在给定的关键字(通常是一个NSString)下存储一个数值(可以是任何类型的对象).然后你就可以用这个关键字来 ...

  7. 数据库 SQL基础

    数据库是用来存取数据的. 数据库类型: ACESS(比较小,存储少) SQL SERVER (.net) MySQL Oracle(java) 数据库:服务.界面 服务是可以操作的后台的程序. 界面是 ...

  8. C++初学者 const使用详解

     关于C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,参考了康建东兄的const使用详解一文,对其中进行了一些补充,写下了本文. 1. const常量 如: cons ...

  9. HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...

  10. C#访问postgresql数据库

    最近开始做C#的DotNet的工作,因为对PostgreSQL数据库比较有兴趣,所以自己研究了一下如何访问PostgreSQL的 数据库的问题. 1.为了访问PostgreSQL数据库,需要从pgfo ...