Go: ///
// File:  0546.go
// Author: ymiyamoto
//
// Created on Tue Mar  6 23:19:41 2018
//
package main

import (
	"fmt"
)

var n, k int

func perm(arr []int) [][]int {
	if len(arr) == 1 {
		return [][]int{arr}
	}

	p := perm(arr[1:])
	var ret [][]int
	for _, val := range p {
		for i := 0; i <= len(val); i++ {
			tmp := make([]int, len(val))
			copy(tmp, val)
			tmp = append(tmp, 0)
			copy(tmp[i+1:], tmp[i:])
			tmp[i] = arr[0]
			ret = append(ret, tmp)
		}
	}

	return ret
}

func permutation(n, k int) [][]int {
	ret := [][]int{}

	for i := 0; i < 1<<uint(n); i++ {
		var selects []int
		for j := 0; j < n; j++ {
			if (i>>uint(j))&1 == 1 {
				selects = append(selects, j)
			}
		}
		if len(selects) == k {
			ret = append(ret, perm(selects)...)
		}
	}

	return ret
}

func main() {
	for {
		fmt.Scan(&n, &k)
		if n == 0 && k == 0 {
			break
		}

		cards := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&cards[i])
		}

		ret := permutation(len(cards), k)
		nums := map[string]bool{}
		for _, val := range ret {
			var num string
			for i := 0; i < len(val); i++ {
				num += cards[val[i]]
			}
			nums[num] = true
		}

		fmt.Println(len(nums))
	}
}

C++: #include<iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	int n, k, result[100], i, p, a, b, c, d, e;
	char card[10][3];
	char number[5000][9];

	for (i = 0;; i++)
	{
		cin >> n;
		cin >> k;

		a = 0;
		b = 0;
		c = 0;
		d = 0;
		e = 0;
		p = 0;

		if (n == 0 && k == 0)
		{
			break;
		}

		for (int a = 0; a < n; a++)
		{
			cin >> card[a];
		}

		if (k == 2)
		{
			result[i] = n*(n - 1);
			for (a; a < n*n; a++)
			{
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				c++;
			}
			for (int a = 0; a < n*n - 1; a++)
			{
				if (atoi(number[a]) != 0)
				{
					for (int b = a + 1; b < n*n; b++)
					{
						if (atoi(number[a]) == atoi(number[b]))
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}

		else if (k == 3)
		{
			result[i] = n*(n - 1)*(n - 2);
			for (int a = 0; a < n*n*n; a++)
			{
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && c != d && d != b)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[d][f] != *"")
							{
								number[a][p] = card[d][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				d++;
			}
			for (int a = 0; a < n*n*n - 1; a++)
			{
				if (atoi(number[a]) != 0)
				{
					for (int b = a + 1; b < n*n*n; b++)
					{
						if (atoi(number[a]) == atoi(number[b]))
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}
	
		else if (k == 4)
		{
			result[i] = n*(n - 1)*(n - 2)*(n - 3);
			for (int a = 0; a < n*n*n*n; a++)
			{
				if (e >= n)
				{
					e = 0;
					d++;
				}
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && b != d && b != e && c != d && c != e && d != e)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[d][f] != *"")
							{
								number[a][p] = card[d][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[e][f] != *"")
							{
								number[a][p] = card[e][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				e++;
			}
		}
		for (int a = 0; a < n*n*n*n - 1; a++)
		{
			if (atoi(number[a]) != 0)
			{
				for (int b = a + 1; b < n*n*n*n; b++)
				{
					if (atoi(number[a]) == atoi(number[b]))
					{
						number[a][0] = *"0";
						number[a][1] = *"";
						result[i]--;
						break;
					}
				}
			}
		}
	}

	for (int a = 0; a < i; a++)
	{
		cout << result[a] << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;

int n, k;

int main(){
	while(1){
		scanf("%d %d", &n, &k);
		if(n == 0 && k == 0) break;

		vector<string> v;
		set<string> num;
		for(int i=0; i<n; i++){
			char tmp[3];
			scanf("%s", tmp);
			v.push_back(tmp);
		}
		sort(v.begin(), v.end());

		do{

			string tmp = "";
			for(int i=0; i<k; i++)
				tmp += v[i];
			num.insert(tmp);

		}while(next_permutation(v.begin(), v.end()));

		printf("%lu\n", num.size());
	}
	return 0;
}

C++: #include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

set<string> s;
bool used[10];
string card[10];
int n,k,u[4];

int dfs(int c){
    if(c==k){
        do{ 
            string cb;
            for(int i=0;i<k;++i) cb +=card[u[i]];
            s.insert(cb);
        }while(next_permutation(card,card+k));
        return 0;
    }
    for(int i=0;i<n;++i){
        if(!used[i]){
            used[i]=1;u[c]=i;
            dfs(c+1);
            used[i]=0;
        }
    }
}
int main(){
    while(cin>>n>>k,n){
        s.clear();
        for(int i=0;i<n;++i) cin>>card[i];
        memset(used,0,sizeof(used));
        sort(card,card+n);
        dfs(0);
        cout<<s.size()<<endl;
    }
    return 0;
}