Number of stringsTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3505 Accepted Submission (s): 855
Problem description A and two b altogether can be composed of three kinds of strings: "ABB", "BAB", "BBA".
Given the number of letters and their corresponding number, calculate how many different strings can be composed altogether.
Input each group of test data divided into two lines, the first behavior N (1<=n<=26), the number of different letters, the second action n the number of a1,a2,..., an (1<=ai<=12), the number of each letter. The test data ends with n=0.
Output for each set of test data, outputs an m that represents the total number of strings.
Sample Input
21 232 2 20
Sample Output
390
The formula can be easily introduced:(N1+N2+N3+...NN)!/(n1!+n2!+...+nn!);
Code:
#include <stdio.h> #include <string.h> #define SIZE 30typedef long long ll; int d[size]; int ans[1000], f[15];v OID multiply (int c) {ans[0] = ans[1] = 1, for (int i = 2, I <= C; ++i) {int r = 0; for (int j = 1; J <= Ans[0]; ++j) {Ans[j] *= i; ans[j] + = r; r = ANS[J]/10; Ans[j]%= 10;} if (r = 0) {while (R) {ans[ans[0]+1] + = r%10; Ans[0] = ans[0]+1; r/= 10;}}} void divide (int n) {for (int i = 0; i < n; ++i) {if (d[i] = = 1) continue, LL r = 0; for (int j = ans[0]; j > 0;--j) {R = r*10 + ans[j]; Ans[j] = (int) (r/f[d[i]]); R%= F[d[i]];} Int j = Ans[0]; while (!ans[j--]); ans[0] = j+1;}} int main () {int n; f[0] = f[1] = 1; for (int i = 2; i < [++i] f[i] = f[i-1]*i; while (scanf ("%d", &n) && N) {int c = 0; memset (ans,0,sizeof (ans)); for (int i = 0; i < n; ++i) {scanf ("%d", &d[i]); c + = D[i];} Multiply (c); Divide (n); for (int i = ans[0]; i > 0; i) printf ("%d", Ans[i]);p UTS (""); return 0;} /*2612 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 Answer: 4304048360075613709828073573963439771561246234874986506307801024608438578682946287839555537779695114697346360347651799406 0086036378690699890922398850719933177533763394328048206442072300423203375709346966736476550911025693768450467040156990933 7603281490150010206952984854343148131819790936625546951803765578418939050054389462227756722335175713361961442027131622012 12368185472256427516516825563136000000000000000000000000*/
with June
HDU 1261-word string permutation combination