首页 技术 正文
技术 2022年11月19日
0 收藏 661 点赞 3,980 浏览 1772 个字

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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900