[UVA][Math] 10338 - Mischievous Children
Problem C
Mischievous Children
Input: standard input
Output: standard output
Time Limit: 1 second
Memory Limit: 32 MB
Adam’sparents put up a sign that says “CONGRATULATIONS”. The sign is so big thatexactly one letter fits on each panel. Some of Adam’s younger cousins got boredduring the reception and decided to rearrange the panels. How many unique wayscan the panels be arranged (counting the original arrangement)?
Input / Output
Thefirst line of input is a single non-negative integer. It indicates the numberof data sets to follow. Its value will be less than 30001.
Eachdata set consists of a single word, in all capital letters. For each word,output the number of unique ways that the letters can be rearranged (countingthe original arrangement). Use the format shown in Sample Output, below.
Eachword will have at most 20 letters. There will be no spaces or otherpunctuation.
Thenumber of arrangements will always be able to fit into an unsigned long int. Note that 12! is thelargest factorial that can fit into an unsignedlong int.
Sample
Sample Input
3
HAPPY
WEDDING
ADAM
Sample Output
Data set 1: 60
Data set 2: 2520
Data set 3: 12
#include <string.h>
int main() {
int t, Case = 0, i;
char str[20];
long long f[22] = {1, 1};
for(i = 2; i <= 20; i++)
f[i] = f[i-1]*i;
scanf("%d", &t);
while(t--) {
scanf("%s", str);
int letter[26] = {}, len = strlen(str);
for(i = 0; str[i]; i++)
letter[str[i]-'A']++;
long long ans = f[len];
for(i = 0; i < 26; i++)
ans /= f[letter[i]];
printf("Data set %d: %lld\n", ++Case, ans);
}
return 0;
}