Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1980

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1980

Shuffling

 

By Gustavo Chermout, UNIFESO BR Brazil

Timelimit: 1

Gabriel is a student of computer science course, he always liked to logic games, an example is the rubik's cube, students are surprised to see how easy to him solve it. Gabriel decided to set up his own game involving logic, the first information he will need to mount the game is how many anagrams can be formed with a certain number of distinct characters without spaces.

As he uses much of your time for programming contest, it ends up not having time to check this, so he need your help.
Your task is, given a word with different characters and without spaces, say how many anagrams can be formed.

 

Input

 

The input consists of several test cases. Each test case will have a single line S with a maximum of 15 characters. The input ends with S = 0 and shouldn't be processed.

 

Output

 

For each test case you should print how many anagrams are possible form with the informed characters.

 

 

 

Input Sample Output Sample

abc
abcde
abcdefg
0

6
120
5040

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <string.h>

int main (void)
{

	char string[20];
	unsigned long long fatorial = 1;
	unsigned short i, contador = 0, tamanho;

	while (1)
	{
		scanf("%s", string);

		if (string[0] == '0')
			break;

		tamanho = strlen(string);

		while (tamanho)
		{

			fatorial *= tamanho--;

		}

		printf("%llu\n", fatorial);
		fatorial = 1;
		
	}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
abc
abcde
abcdefg
0

Output

x
+
cmd
6
120
5040

#2 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;


ll fat[16];

void calc()
{
	fat[0] = fat[1] = 1;
	
	for (int i = 2; i  < = 15; ++i)
		fat[i] = i * fat[i - 1];
}

ll solve(int n)
{
	return fat[n];
}

int main()
{
	string s;
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	int freq[26];
	calc();
	while (1)
	{
		cin >> s;
		
		if (s == "0") return 0;
		
		cout << solve(s.size()) << '\n';
	}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
abc
abcde
abcdefg
0

Output

x
+
cmd
6
120
5040

#3 Code Example with Java Programming

Code - Java Programming


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
    static Reader in = new Reader(System.in);
    static Writer out = new Writer(System.out);
    static final String[] FACTORIALS = {"1", "1", "2", "6", "24", "120", "720", "5040", "40320", "362880", "3628800", "39916800", "479001600", "6227020800", "87178291200", "1307674368000"};
    
    public static void main(String[] args) throws IOException {
        String s;
        while (!(s = in.next()).equals("0")) {
            out.println(FACTORIALS[s.length()]);
        }
        in.close();
        out.flush();
        out.close();
    }

    ////////////////////////////////////////////////////////////////////////////
    /////////////////////////////  INPUT / OUTPUT  /////////////////////////////
    ////////////////////////////////////////////////////////////////////////////
    static class Reader implements Closeable {
        
        private final BufferedReader reader;
        private StringTokenizer tokenizer;
        
        public Reader(InputStream input) {
            reader = new BufferedReader(
                    new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }
        
        private StringTokenizer getTokenizer() throws IOException {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                String line = nextLine();
                if (line == null) {
                    return null;
                }
                tokenizer = new StringTokenizer(line);
            }
            return tokenizer;
        }
        
        public boolean hasNext() throws IOException {
            return getTokenizer() != null;
        }
        
        public String next() throws IOException {
            return hasNext() ? tokenizer.nextToken() : null;
        }
        
        public String nextLine() throws IOException {
            tokenizer = null;
            return reader.readLine();
        }
        
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        
        public float nextFloat() throws IOException {
            return Float.parseFloat(next());
        }
        
        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
        
        public String[] nextStringArray(int size) throws IOException {
            String[] array = new String[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = next();
            }
            return array;
        }
        
        public int[] nextIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = nextInt();
            }
            return array;
        }
        
        public long[] nextLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = nextLong();
            }
            return array;
        }
        
        public double[] nextDoubleArray(int size) throws IOException {
            double[] array = new double[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = nextDouble();
            }
            return array;
        }
        
        @Override
        public void close() throws IOException {
            tokenizer = null;
            reader.close();
        }
    }
    
    static class Writer {
        
        private final PrintWriter writer;
        
        public Writer(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }
        
        public void print(Object... objects) {
            for (int i = 0; i  <  objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }
        
        public void println(Object... objects) {
            print(objects);
            writer.println();
        }
        
        public void close() {
            writer.close();
        }
        
        public void flush() {
            writer.flush();
        }
        
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
abc
abcde
abcdefg
0

Output

x
+
cmd
6
120
5040

#4 Code Example with Python Programming

Code - Python Programming


def fat(n):
    if n == 0: return 1
    return n * fat(n - 1)

while True: 
    e = str(input()).strip()
    if e == '0': break
    print(fat(len(e)))
Copy The Code & Try With Live Editor

Input

x
+
cmd
abc
abcde
abcdefg
0

Output

x
+
cmd
6
120
5040
Advertisements

Demonstration


Previous
#1973 Beecrowd Online Judge Solution 1973 Star Trek Solution in C++, Java, Js and Python
Next
#1984 Beecrowd Online Judge Solution 1984 The Pronalância Puzzle Solution in C++, Java, Js and Python